BlockerHelper/backends/xdp.pm

filter: Statementstmt 38.5% 93 / 241 Branchbran 31.5% 24 / 76 Conditioncond 41.6% 5 / 12 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 63.1% 12 / 19 Podpod 100% 13 / 13 totaltotal 40.2% 148 / 368 CC 52 SCAR 31.5
CC
52
Cov
37%
re_init
42.0
flush
42.0
ban
41.3
CRAP 23.3
« BlockerHelper/backends/aws_wafv2.pm ↑ Summary BlockerHelper/backends/npf.pm »
1 package Net::Firewall::BlockerHelper::backends::xdp;
2  
3 1 ▶use 5.006;
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 base 'Error::Helper';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Regexp::IPv4 qw($IPv4_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Regexp::IPv6 qw($IPv6_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Net::Firewall::BlockerHelper::backends::xdp - XDP/eBPF backend for Net::Firewall::BlockerHelper.
13  
14 =head1 VERSION
15  
16 Version 0.1.0
17  
18 =cut
19  
20 1 our $VERSION = '0.1.0';
21  
22 =head1 SYNOPSIS
23  
24 use Net::Firewall::BlockerHelper::backends::xdp;
25  
26 my $backend;
27 eval {
28 $backend = Net::Firewall::BlockerHelper::backends::xdp->new(
29 name => 'derp',
30 options => {
31 interfaces => ['eth0'],
32 },
33 );
34 };
35 if ($@) {
36 print 'Error: '
37 . $Error::Helper::error
38 . "\nError String: "
39 . $Error::Helper::errorString
40 . "\nError Flag: "
41 . $Error::Helper::errorFlag . "\n";
42 }
43  
44 $backend->init;
45  
46 $backend->ban(ban=>'1.2.3.4');
47 $backend->ban(ban=>'4.3.2.1');
48  
49 use Data::Dumper;
50 print Dumper($backend->list);
51  
52 $backend->unban(ban=>'4.3.2.1');
53  
54 $backend->teardown;
55  
56 =head1 DESCRIPTION
57  
58 This backend drops packets using XDP/eBPF via the C<xdp-filter> tool from
59 xdp-tools.
60  
61 C<xdp-filter> attaches an XDP program to one or more interfaces and maintains
62 an IP blocklist in a BPF map. This backend loads the program with a default
63 C<allow> policy so only the IPs explicitly added to the blocklist are dropped.
64  
65 Requires C<xdp-filter> to be installed and in the C<PATH> of the process,
66 which must have sufficient privileges to run it.
67  
68 =head1 METHODS
69  
70 =head2 new
71  
72 Initiates the the object.
73  
74 - options :: A hash of options to pass to the backend. See below for
75 further info.
76 - Default :: {}
77  
78 - ports :: Not used by this backend but is accepted for parity with
79 Net::Firewall::BlockerHelper.
80 - Default :: []
81  
82 - protocols :: Not used by this backend but is accepted for parity with
83 Net::Firewall::BlockerHelper.
84 - Default :: []
85  
86 - prefix :: Not used by this backend but is accepted for parity with
87 Net::Firewall::BlockerHelper.
88 - Default :: kur
89  
90 - name :: Not used but is required by Net::Firewall::BlockerHelper.
91 - Default :: undef
92  
93 The options hash accepts the following.
94  
95 - interfaces :: An array ref of interface names to load the XDP program
96 onto, e.g. ['eth0']. This is required and must be a non-empty
97 array ref.
98 - Default :: undef
99  
100 - xdp_filter_cmd :: The path to the xdp-filter binary.
101 - Default :: xdp-filter
102  
103 - mode :: The match mode passed to the 'ip' command via '-m', either
104 'src' or 'dst'.
105 - Default :: src
106  
107 - xdp_mode :: The XDP attach mode used when loading the program, passed to
108 the 'load' command via '-m'. One of 'native', 'skb', 'hw', or
109 'unspecified'.
110 - Default :: native
111  
112 All errors are considered fatal, meaning if new fails it will die.
113  
114 my $backend;
115 eval {
116 $backend = Net::Firewall::BlockerHelper::backends::xdp->new(
117 name => 'derp',
118 options => {
119 interfaces => ['eth0'],
120 },
121 );
122 };
123 if ($@) {
124 print 'Error: '
125 . $Error::Helper::error
126 . "\nError String: "
127 . $Error::Helper::errorString
128 . "\nError Flag: "
129 . $Error::Helper::errorFlag . "\n";
130 }
131  
132 =cut
133  
134 sub new {
135 3 ▶ my ( $blank, %opts ) = @_;
Subroutine new: called CC=10 SCAR=24.9
Pod documented
136  
137 3 my $self = {
138 perror => undef,
139 error => undef,
140 errorLine => undef,
141 errorFilename => undef,
142 errorString => "",
143 errorExtra => {
144 all_errors_fatal => 1,
145 # all_fatal is what Error::Helper 2.1.0 actually checks; all_errors_fatal
146 # is kept for the name documented in its POD
147 all_fatal => 1,
148 flags => {
149 1 => 'notInited',
150 8 => 'optionsNotHash',
151 9 => 'noBanItem',
152 10 => 'banItemNotIP',
153 12 => 'backendInitError',
154 13 => 'banFailed',
155 14 => 'unbanFailed',
156 15 => 'listFailed',
157 16 => 'reInitFailed',
158 17 => 'teardownFailed',
159 18 => 'alreadyInited',
160 20 => 'modeInvalid',
161 24 => 'checkFailed',
162 25 => 'flushFailed',
163 30 => 'interfacesInvalid',
164 31 => 'banCidrFailed',
165 32 => 'unbanCidrFailed',
166 33 => 'cidrItemNotCidr',
167 34 => 'cidrNotSupported',
168 35 => 'listCidrFailed',
169 },
170 fatal_flags => {},
171 perror_not_fatal => 0,
172 },
173 options => {
174 xdp_filter_cmd => 'xdp-filter',
175 mode => 'src',
176 xdp_mode => 'native',
177 },
178 ports => [],
179 protocols => [],
180 testing => undef,
181 test_data => undef,
182 prefix => 'kur',
183 frontend_obj => undef,
184 inited => 0,
185 banned => {},
186 cidr_supported => 0,
187 };
188 3 bless $self;
189  
190 # used internally for testing
191 3 ▶ if ( defined( $opts{testing} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"testing"}) 3 0 3
192 3 $self->{testing} = $opts{testing};
193 }
194 3 ▶ if ( defined( $opts{frontend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"frontend_obj"}) 3 0 3
195 3 $self->{frontend_obj} = $opts{frontend_obj};
196 }
197  
198 3 ▶ if ( defined( $opts{options} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"options"}) 3 0 3
199 3 ▶ if ( ref( $opts{options} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"options"} ne "HASH") 0 3 3
200 0 $self->{perror} = 1;
201 0 $self->{error} = 8;
202 0 $self->{errorString} = 'ref for options is "' . ref( $opts{options} ) . '" and not HASH';
203 0 $self->warn;
204 0 return $self;
205 }
206  
207 # merge in the passed options, keeping the defaults for anything not set
208 3 foreach my $key ( keys( %{ $opts{options} } ) ) {
209 5 $self->{options}{$key} = $opts{options}{$key};
210 }
211 }
212  
213 # interfaces is required and must be a non-empty array ref
214 3 ▶ if ( !defined( $self->{options}{interfaces} )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"interfaces"} or ref $self->{"options"}{"interfaces"} ne "ARRAY" or not @{$self->{"options"}{"interfaces"};}) 1 2 3
Condition50%3/6
not defined $self->{"options"}{"interfaces"} or ref $self->{"options"}{"interfaces"} ne "ARRAY" or not @{$self->{"options"}{"interfaces"};}
l!l&&r!l&&!r
012
not defined $self->{"options"}{"interfaces"} or ref $self->{"options"}{"interfaces"} ne "ARRAY"
l!l&&r!l&&!r
003
MC/DC33%1/3
not defined $self->{"options"}{"interfaces"} or ref $self->{"options"}{"interfaces"} ne "ARRAY" or not @{$self->{"options"}{"interfaces"};}
not defined $self->{"options"}{"interfaces"} ref $self->{"options"}{"interfaces"} ne "ARRAY" not @{$self->{"options"}{"interfaces"};}
Truth table50%2/4
not(A) or B or not(C)
A= not defined $self->{"options"}{"interfaces"}B= ref $self->{"options"}{"interfaces"} ne "ARRAY"C= not @{$self->{"options"}{"interfaces"};}
ABCresult
1XX1
01X1
0011
0000
215 || ref( $self->{options}{interfaces} ) ne 'ARRAY'
216 3 || !@{ $self->{options}{interfaces} } )
217 {
218 1 $self->{perror} = 1;
219 1 $self->{error} = 30;
220 $self->{errorString}
221 1 = 'interfaces is required and must be a non-empty array ref';
222 1 $self->warn;
223 0 return $self;
224 }
225  
226 # mode drives the 'ip' command's -m flag and must be src or dst
227 2 my %valid_modes = ( src => 1, dst => 1 );
228 2 ▶ if ( !$valid_modes{ $self->{options}{mode} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($valid_modes{$self->{"options"}{"mode"}}) 1 1 2
229 1 $self->{perror} = 1;
230 1 $self->{error} = 20;
231 $self->{errorString}
232 1 = 'mode is "' . $self->{options}{mode} . '" and not one of "src" or "dst"';
233 1 $self->warn;
234 0 return $self;
235 }
236  
237 1 $self->{name} = $opts{name};
238  
239 1 return $self;
240 } ## end sub new
241  
242 =head2 init
243  
244 Initiates the backend. For each configured interface this loads the
245 C<xdp-filter> program with the ipv4 and ipv6 features and a default C<allow>
246 policy, so only IPs added to the blocklist are dropped.
247  
248 No arguments are taken.
249  
250 If called a second time, it will error.
251  
252 $backend->init;
253  
254 =cut
255  
256 sub init {
257 1 ▶ my ( $self, %opts ) = @_;
Subroutine init: called CC=6 SCAR=23.0
Pod documented
258  
259 1 $self->errorblank;
260  
261 1 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 1 1
262 0 $self->{error} = 18;
263 0 $self->{errorString} = 'backend has already been inited';
264 0 $self->warn;
265 }
266  
267 1 my $cmd = $self->{options}{xdp_filter_cmd};
268 1 my $xdp_mode = $self->{options}{xdp_mode};
269  
270 1 my @commands;
271 1 foreach my $iface ( @{ $self->{options}{interfaces} } ) {
272 2 push( @commands,
273 $cmd . ' load -f ipv4,ipv6 -p allow -m ' . $xdp_mode . ' ' . $iface );
274 }
275  
276 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
277 1 $self->{frontend_obj}->{test_data} = \@commands;
278 } else {
279 0 foreach my $command (@commands) {
280 0 my $output = `$command 2>&1`;
281 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
282 0 $self->{error} = 12;
283 $self->{errorString}
284 0 = 'Init failed... command "' . $command . '" resulted in... ' . $output;
285 0 $self->warn;
286 }
287 }
288 }
289  
290 1 $self->{inited} = 1;
291 } ## end sub init
292  
293 =head2 ban
294  
295 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address
296 and lowercased, then added to the XDP blocklist by running
297 C<< xdp-filter ip <ip> -m <mode> >>. Banning an already banned IP is a noop.
298  
299 $backend->ban(ban => $ip);
300  
301 =cut
302  
303 sub ban {
304 1 ▶ my ( $self, %opts ) = @_;
Subroutine ban: called CC=10 SCAR=37.2
Pod documented
305  
306 1 $self->errorblank;
307  
308 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
309 0 $self->{error} = 1;
310 0 $self->{errorString} = 'backend has not been inited';
311 0 $self->warn;
312 0 return;
313 }
314  
315 1 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 1 1
elsif (ref $opts{'ban'} ne '') { } 0 1 1
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::xdp::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::xdp::IPv6_re\z/) { } 0 1 1
316 0 $self->{error} = 9;
317 0 $self->{errorString} = 'Nothing specified for the value ban';
318 0 $self->warn;
319 0 return;
320 } elsif ( ref( $opts{ban} ) ne '' ) {
321 0 $self->{error} = 10;
322 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
323 0 $self->warn;
324 0 return;
325 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition33%1/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::xdp::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::xdp::IPv6_re\z/
l!l&&r!l&&!r
100
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::xdp::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::xdp::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::xdp::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::xdp::IPv6_re\z/
Truth table33%1/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::xdp::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::xdp::IPv6_re\z/
ABresult
1X1
011
000
326 && $opts{ban} !~ /\A$IPv6_re\z/ )
327 {
328 0 $self->{error} = 10;
329 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
330 0 $self->warn;
331 0 return;
332 }
333  
334 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
335 1 $opts{ban} = lc( $opts{ban} );
336  
337 1 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 1 1
338 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
339 0 $self->{frontend_obj}->{test_data} = 'already banned';
340 }
341 0 return;
342 }
343  
344 my $command
345 1 = $self->{options}{xdp_filter_cmd} . ' ip ' . $opts{ban} . ' -m ' . $self->{options}{mode};
346  
347 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
348 1 $self->{frontend_obj}->{test_data} = $command;
349 } else {
350 0 my $output = `$command 2>&1`;
351 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
352 0 $self->{error} = 13;
353 0 $self->{errorString} = 'Ban failed... command "' . $command . '" resulted in... ' . $output;
354 0 $self->warn;
355 }
356 }
357  
358 1 $self->{banned}{ $opts{ban} } = 1;
359 } ## end sub ban
360  
361 =head2 unban
362  
363 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
364 and lowercased, then removed from the XDP blocklist by running
365 C<< xdp-filter ip <ip> -m <mode> -r >>. Unbanning an IP that is not banned
366 is a noop.
367  
368 $backend->unban(ban => $ip);
369  
370 =cut
371  
372 sub unban {
373 1 ▶ my ( $self, %opts ) = @_;
Subroutine unban: called CC=10 SCAR=37.2
Pod documented
374  
375 1 $self->errorblank;
376  
377 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
378 0 $self->{error} = 1;
379 0 $self->{errorString} = 'backend has not been inited';
380 0 $self->warn;
381 0 return;
382 }
383  
384 1 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 1 1
elsif (ref $opts{'ban'} ne '') { } 0 1 1
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::xdp::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::xdp::IPv6_re\z/) { } 0 1 1
385 0 $self->{error} = 9;
386 0 $self->{errorString} = 'Nothing specified for the value ban';
387 0 $self->warn;
388 0 return;
389 } elsif ( ref( $opts{ban} ) ne '' ) {
390 0 $self->{error} = 10;
391 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
392 0 $self->warn;
393 0 return;
394 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition33%1/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::xdp::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::xdp::IPv6_re\z/
l!l&&r!l&&!r
100
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::xdp::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::xdp::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::xdp::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::xdp::IPv6_re\z/
Truth table33%1/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::xdp::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::xdp::IPv6_re\z/
ABresult
1X1
011
000
395 && $opts{ban} !~ /\A$IPv6_re\z/ )
396 {
397 0 $self->{error} = 10;
398 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
399 0 $self->warn;
400 0 return;
401 }
402  
403 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
404 1 $opts{ban} = lc( $opts{ban} );
405  
406 1 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 1 1
407 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
408 0 $self->{frontend_obj}->{test_data} = 'not banned';
409 }
410 0 return;
411 }
412  
413 my $command
414 1 = $self->{options}{xdp_filter_cmd} . ' ip ' . $opts{ban} . ' -m ' . $self->{options}{mode} . ' -r';
415  
416 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
417 1 $self->{frontend_obj}->{test_data} = $command;
418 } else {
419 0 my $output = `$command 2>&1`;
420 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
421 0 $self->{error} = 14;
422 0 $self->{errorString} = 'Unban failed... command "' . $command . '" resulted in... ' . $output;
423 0 $self->warn;
424 }
425 }
426  
427 1 delete( $self->{banned}{ $opts{ban} } );
428 } ## end sub unban
429  
430 =head2 ban_cidr
431  
432 CIDR bans are not supported by this backend; this always sets the
433 cidrNotSupported error.
434  
435 $backend->ban_cidr(ban => '1.2.3.0/24');
436  
437 =cut
438  
439 sub ban_cidr {
440 0 ▶ my ( $self, %opts ) = @_;
Subroutine ban_cidr: not called CC=1 SCAR=6.9
Pod documented
441  
442 0 $self->errorblank;
443  
444 0 $self->{error} = 34;
445 0 $self->{errorString} = 'the ' . __PACKAGE__ . ' backend does not support CIDR bans';
446 0 $self->warn;
447  
448 0 return;
449 } ## end sub ban_cidr
450  
451 =head2 unban_cidr
452  
453 CIDR bans are not supported by this backend; this always sets the
454 cidrNotSupported error.
455  
456 $backend->unban_cidr(ban => '1.2.3.0/24');
457  
458 =cut
459  
460 sub unban_cidr {
461 0 ▶ my ( $self, %opts ) = @_;
Subroutine unban_cidr: not called CC=1 SCAR=6.9
Pod documented
462  
463 0 $self->errorblank;
464  
465 0 $self->{error} = 34;
466 0 $self->{errorString} = 'the ' . __PACKAGE__ . ' backend does not support CIDR bans';
467 0 $self->warn;
468  
469 0 return;
470 } ## end sub unban_cidr
471  
472 =head2 list_cidr
473  
474 CIDR bans are not supported by this backend, so this always returns an empty
475 list.
476  
477 my @banned_cidrs = $backend->list_cidr;
478  
479 =cut
480  
481 sub list_cidr {
482 0 ▶ my ( $self, %opts ) = @_;
Subroutine list_cidr: not called CC=1 SCAR=6.9
Pod documented
483  
484 0 $self->errorblank;
485  
486 0 return ();
487 }
488  
489 =head2 list
490  
491 List banned IPs. Returns an array of the currently banned IPs.
492  
493 my @banned = $backend->list;
494  
495 =cut
496  
497 sub list {
498 0 ▶ my ( $self, %opts ) = @_;
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
499  
500 0 $self->errorblank;
501  
502 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
503 0 $self->{frontend_obj}->{test_data} = 'list';
504 }
505  
506 0 return keys( %{ $self->{banned} } );
507 }
508  
509 =head2 re_init
510  
511 Tells the backend to re-init it's self.
512  
513 This will call teardown and init again. After that it will re-add all
514 previously added bans.
515  
516 $backend->re_init;
517  
518 =cut
519  
520 sub re_init {
521 0 ▶ my ( $self, %opts ) = @_;
Subroutine re_init: not called CC=6 SCAR=37.4
Pod documented
522  
523 0 $self->errorblank;
524  
525 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
526 0 $self->{error} = 1;
527 0 $self->{errorString} = 'backend has not been inited';
528 0 $self->warn;
529 0 return;
530 }
531  
532 # teardown is best effort here as a partially or fully wiped setup is
533 # exactly what re_init needs to recover from; init cleans up any remnants
534 {
535 0 local $@;
536 0 eval { $self->teardown; };
537 }
538 0 $self->init;
539  
540 0 my @to_re_ban = keys( %{ $self->{banned} } );
541  
542 0 my @re_init_test_data;
543 0 foreach my $item (@to_re_ban) {
544 my $command
545 0 = $self->{options}{xdp_filter_cmd} . ' ip ' . $item . ' -m ' . $self->{options}{mode};
546  
547 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
548 0 push( @re_init_test_data, $command );
549 } else {
550 0 my $output = `$command 2>&1`;
551 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
552 0 $self->{error} = 13;
553 0 $self->{errorString} = 'Ban failed... command "' . $command . '" resulted in... ' . $output;
554 0 $self->warn;
555 }
556 }
557 } ## end foreach my $item (@to_re_ban)
558  
559 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
560 0 $self->{frontend_obj}->{test_data} = \@re_init_test_data;
561 }
562  
563 0 $self->{inited} = 1;
564 } ## end sub re_init
565  
566 =head2 teardown
567  
568 Tears down the setup for the backend.
569  
570 This will unload the XDP program from each configured interface.
571  
572 $backend->teardown;
573  
574 =cut
575  
576 sub teardown {
577 1 ▶ my ( $self, %opts ) = @_;
Subroutine teardown: called CC=5 SCAR=19.8
Pod documented
578  
579 1 $self->errorblank;
580  
581 1 $self->{inited} = 0;
582  
583 1 my $cmd = $self->{options}{xdp_filter_cmd};
584  
585 1 my @commands;
586 1 foreach my $iface ( @{ $self->{options}{interfaces} } ) {
587 2 push( @commands, $cmd . ' unload ' . $iface );
588 }
589  
590 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
591 1 $self->{frontend_obj}->{test_data} = \@commands;
592 } else {
593 0 foreach my $command (@commands) {
594 0 my $output = `$command 2>&1`;
595 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
596 0 $self->{error} = 17;
597 0 $self->{errorString} = 'Teardown failed... command "' . $command . '" resulted in... ' . $output;
598 0 $self->warn;
599 }
600 }
601 }
602 } ## end sub teardown
603  
604 =head2 stop
605  
606 Alias for L</teardown>, provided for parity with the fail2ban C<actionstop>
607 concept.
608  
609 $backend->stop;
610  
611 =cut
612  
613 sub stop {
614 0 ▶ my ( $self, %opts ) = @_;
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
615  
616 0 return $self->teardown(%opts);
617 }
618  
619 =head2 check
620  
621 Runs C<xdp-filter status> to verify the setup is still in place. To be
622 considered healthy the command must exit zero and every configured interface
623 must be listed as loaded in its output; as the status is global, the exit
624 code alone would miss a single interface having been unloaded. This is the
625 equivalent of fail2ban's C<actioncheck>.
626  
627 if ( !$backend->check ) {
628 $backend->re_init;
629 }
630  
631 =cut
632  
633 sub check {
634 2 ▶ my ( $self, %opts ) = @_;
Subroutine check: called CC=5 SCAR=24.7
Pod documented
635  
636 2 $self->errorblank;
637  
638 2 my $command = $self->{options}{xdp_filter_cmd} . ' status';
639  
640 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
641 2 $self->{frontend_obj}->{test_data} = $command;
642 2 return 1;
643 }
644  
645 0 my $output = `$command 2>&1`;
646 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
647 0 return 0;
648 }
649  
650 # the status is global, so make sure each configured interface is still
651 # listed as loaded; a lone unloaded interface leaves the exit code zero
652 0 foreach my $iface ( @{ $self->{options}{interfaces} } ) {
653 0 ▶ if ( $output !~ /^\s*\Q$iface\E\s/m ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($output =~ /^\s*\Q$iface\E\s/m) 0 0 0
654 0 return 0;
655 }
656 }
657  
658 0 return 1;
659 } ## end sub check
660  
661 =head2 flush
662  
663 Removes all currently banned IPs at once by removing each one from the XDP
664 blocklist. This is the equivalent of fail2ban's C<actionflush>.
665  
666 $backend->flush;
667  
668 =cut
669  
670 sub flush {
671 0 ▶ my ( $self, %opts ) = @_;
Subroutine flush: not called CC=6 SCAR=37.4
Pod documented
672  
673 0 $self->errorblank;
674  
675 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
676 0 $self->{error} = 1;
677 0 $self->{errorString} = 'backend has not been inited';
678 0 $self->warn;
679 0 return;
680 }
681  
682 0 my @commands;
683 0 foreach my $item ( keys( %{ $self->{banned} } ) ) {
684 push( @commands,
685 0 $self->{options}{xdp_filter_cmd} . ' ip ' . $item . ' -m ' . $self->{options}{mode} . ' -r' );
686 }
687  
688 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
689 0 $self->{frontend_obj}->{test_data} = \@commands;
690 } else {
691 0 foreach my $command (@commands) {
692 0 my $output = `$command 2>&1`;
693 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
694 0 $self->{error} = 25;
695 0 $self->{errorString} = 'Flush failed... command "' . $command . '" resulted in... ' . $output;
696 0 $self->warn;
697 }
698 }
699 }
700  
701 0 $self->{banned} = {};
702 } ## end sub flush
703  
704 =head1 ERROR CODES / FLAGS
705  
706 Error handling is provided by L<Error::Helper>. All
707 errors are considered fatal.
708  
709 =head2 1, notInited
710  
711 The backend has not been inited yet.
712  
713 =head2 8, optionsNotHash
714  
715 The item passed to new for options is not a hash.
716  
717 =head2 9, noBanItem
718  
719 No IP specified to ban or unban.
720  
721 =head2 10, banItemNotIP
722  
723 The item to ban is not an IP. Either wrong ref type or regexp
724 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
725  
726 =head2 12, backendInitError
727  
728 Failed to init the backend.
729  
730 =head2 13, banFailed
731  
732 Failed to ban the item.
733  
734 =head2 14, unbanFailed
735  
736 Failed to unban the item.
737  
738 =head2 15, listFailed
739  
740 Failed to get a list of bans.
741  
742 =head2 16, reInitFailed
743  
744 Failed to re_init the backend.
745  
746 =head2 17, teardownFailed
747  
748 Failed to teardown the backend.
749  
750 =head2 18, alreadyInited
751  
752 init called, but the backend has already been inited.
753  
754 =head2 20, modeInvalid
755  
756 The option mode is not 'src' or 'dst'.
757  
758 =head2 24, checkFailed
759  
760 The backend check raised an error.
761  
762 =head2 25, flushFailed
763  
764 Failed to flush the bans.
765  
766 =head2 30, interfacesInvalid
767  
768 The interfaces option is either undef, not an array ref, or an empty array
769 ref. It is required and must be a non-empty array ref.
770  
771 =head2 31, banCidrFailed
772  
773 Failed to ban the CIDR range.
774  
775 =head2 32, unbanCidrFailed
776  
777 Failed to unban the CIDR range.
778  
779 =head2 33, cidrItemNotCidr
780  
781 The item to ban is not a CIDR range. Either wrong ref type or it is not an
782 IPv4 or IPv6 address followed by a prefix length valid for its family.
783  
784 =head2 34, cidrNotSupported
785  
786 The backend does not support CIDR bans.
787  
788 =head2 35, listCidrFailed
789  
790 Failed to get a list of CIDR bans.
791  
792 =head1 AUTHOR
793  
794 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
795  
796 =head1 BUGS
797  
798 Please report any bugs or feature requests to C<bug-net-firewall-blockerhelper at rt.cpan.org>, or through
799 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Firewall-BlockerHelper>. I will be notified, and then you'll
800 automatically be notified of progress on your bug as I make changes.
801  
802  
803  
804  
805 =head1 SUPPORT
806  
807 You can find documentation for this module with the perldoc command.
808  
809 perldoc Net::Firewall::BlockerHelper
810  
811  
812 You can also look for information at:
813  
814 =over 4
815  
816 =item * RT: CPAN's request tracker (report bugs here)
817  
818 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Firewall-BlockerHelper>
819  
820 =item * Search CPAN
821  
822 L<https://metacpan.org/release/Net-Firewall-BlockerHelper>
823  
824 =back
825  
826  
827 =head1 ACKNOWLEDGEMENTS
828  
829  
830 =head1 LICENSE AND COPYRIGHT
831  
832 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
833  
834 This is free software, licensed under:
835  
836 The GNU Lesser General Public License, Version 2.1, February 1999
837  
838  
839 =cut
840  
841 1 1; # End of Net::Firewall::BlockerHelper
« BlockerHelper/backends/aws_wafv2.pm ↑ Summary BlockerHelper/backends/npf.pm »