BlockerHelper/backends/xdp.pm

filter: Statementstmt 39.4% 93 / 236 Branchbran 32.4% 24 / 74 Conditioncond 41.6% 5 / 12 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 63.1% 12 / 19 Podpod 100% 13 / 13 totaltotal 41.0% 148 / 361 CC 51 SCAR 31.0
CC
51
Cov
38%
flush
42.0
ban
41.3
unban
41.3
CRAP 22.2
« BlockerHelper/backends/npf.pm ↑ Summary BlockerHelper/backends/azure.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.2.0
17  
18 =cut
19  
20 1 our $VERSION = '0.2.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 3 ▶sub new {
Subroutine new: called CC=10 SCAR=24.9
Pod documented
135 3 my ( $blank, %opts ) = @_;
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 1 ▶sub init {
Subroutine init: called CC=6 SCAR=23.0
Pod documented
257 1 my ( $self, %opts ) = @_;
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 1 ▶sub ban {
Subroutine ban: called CC=10 SCAR=37.2
Pod documented
304 1 my ( $self, %opts ) = @_;
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 1 ▶sub unban {
Subroutine unban: called CC=10 SCAR=37.2
Pod documented
373 1 my ( $self, %opts ) = @_;
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 0 ▶sub ban_cidr {
Subroutine ban_cidr: not called CC=1 SCAR=6.9
Pod documented
440 0 my ( $self, %opts ) = @_;
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 0 ▶sub unban_cidr {
Subroutine unban_cidr: not called CC=1 SCAR=6.9
Pod documented
461 0 my ( $self, %opts ) = @_;
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 0 ▶sub list_cidr {
Subroutine list_cidr: not called CC=1 SCAR=6.9
Pod documented
482 0 my ( $self, %opts ) = @_;
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 0 ▶sub list {
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
498 0 my ( $self, %opts ) = @_;
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 0 ▶sub re_init {
Subroutine re_init: not called CC=5 SCAR=34.0
Pod documented
521 0 my ( $self, %opts ) = @_;
522  
523 0 $self->errorblank;
524  
525 # teardown is best effort here as a partially or fully wiped setup is
526 # exactly what re_init needs to recover from; init cleans up any remnants
527 {
528 0 local $@;
529 0 eval { $self->teardown; };
530 }
531 0 $self->init;
532  
533 0 my @to_re_ban = keys( %{ $self->{banned} } );
534  
535 0 my @re_init_test_data;
536 0 foreach my $item (@to_re_ban) {
537 my $command
538 0 = $self->{options}{xdp_filter_cmd} . ' ip ' . $item . ' -m ' . $self->{options}{mode};
539  
540 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
541 0 push( @re_init_test_data, $command );
542 } else {
543 0 my $output = `$command 2>&1`;
544 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
545 0 $self->{error} = 13;
546 0 $self->{errorString} = 'Ban failed... command "' . $command . '" resulted in... ' . $output;
547 0 $self->warn;
548 }
549 }
550 } ## end foreach my $item (@to_re_ban)
551  
552 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
553 0 $self->{frontend_obj}->{test_data} = \@re_init_test_data;
554 }
555  
556 0 $self->{inited} = 1;
557 } ## end sub re_init
558  
559 =head2 teardown
560  
561 Tears down the setup for the backend.
562  
563 This will unload the XDP program from each configured interface.
564  
565 $backend->teardown;
566  
567 =cut
568  
569 1 ▶sub teardown {
Subroutine teardown: called CC=5 SCAR=19.8
Pod documented
570 1 my ( $self, %opts ) = @_;
571  
572 1 $self->errorblank;
573  
574 1 $self->{inited} = 0;
575  
576 1 my $cmd = $self->{options}{xdp_filter_cmd};
577  
578 1 my @commands;
579 1 foreach my $iface ( @{ $self->{options}{interfaces} } ) {
580 2 push( @commands, $cmd . ' unload ' . $iface );
581 }
582  
583 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
584 1 $self->{frontend_obj}->{test_data} = \@commands;
585 } else {
586 0 foreach my $command (@commands) {
587 0 my $output = `$command 2>&1`;
588 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
589 0 $self->{error} = 17;
590 0 $self->{errorString} = 'Teardown failed... command "' . $command . '" resulted in... ' . $output;
591 0 $self->warn;
592 }
593 }
594 }
595 } ## end sub teardown
596  
597 =head2 stop
598  
599 Alias for L</teardown>, provided for parity with the fail2ban C<actionstop>
600 concept.
601  
602 $backend->stop;
603  
604 =cut
605  
606 0 ▶sub stop {
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
607 0 my ( $self, %opts ) = @_;
608  
609 0 return $self->teardown(%opts);
610 }
611  
612 =head2 check
613  
614 Runs C<xdp-filter status> to verify the setup is still in place. To be
615 considered healthy the command must exit zero and every configured interface
616 must be listed as loaded in its output; as the status is global, the exit
617 code alone would miss a single interface having been unloaded. This is the
618 equivalent of fail2ban's C<actioncheck>.
619  
620 if ( !$backend->check ) {
621 $backend->re_init;
622 }
623  
624 =cut
625  
626 2 ▶sub check {
Subroutine check: called CC=5 SCAR=24.7
Pod documented
627 2 my ( $self, %opts ) = @_;
628  
629 2 $self->errorblank;
630  
631 2 my $command = $self->{options}{xdp_filter_cmd} . ' status';
632  
633 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
634 2 $self->{frontend_obj}->{test_data} = $command;
635 2 return 1;
636 }
637  
638 0 my $output = `$command 2>&1`;
639 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
640 0 return 0;
641 }
642  
643 # the status is global, so make sure each configured interface is still
644 # listed as loaded; a lone unloaded interface leaves the exit code zero
645 0 foreach my $iface ( @{ $self->{options}{interfaces} } ) {
646 0 ▶ if ( $output !~ /^\s*\Q$iface\E\s/m ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($output =~ /^\s*\Q$iface\E\s/m) 0 0 0
647 0 return 0;
648 }
649 }
650  
651 0 return 1;
652 } ## end sub check
653  
654 =head2 flush
655  
656 Removes all currently banned IPs at once by removing each one from the XDP
657 blocklist. This is the equivalent of fail2ban's C<actionflush>.
658  
659 $backend->flush;
660  
661 =cut
662  
663 0 ▶sub flush {
Subroutine flush: not called CC=6 SCAR=37.4
Pod documented
664 0 my ( $self, %opts ) = @_;
665  
666 0 $self->errorblank;
667  
668 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
669 0 $self->{error} = 1;
670 0 $self->{errorString} = 'backend has not been inited';
671 0 $self->warn;
672 0 return;
673 }
674  
675 0 my @commands;
676 0 foreach my $item ( keys( %{ $self->{banned} } ) ) {
677 push( @commands,
678 0 $self->{options}{xdp_filter_cmd} . ' ip ' . $item . ' -m ' . $self->{options}{mode} . ' -r' );
679 }
680  
681 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
682 0 $self->{frontend_obj}->{test_data} = \@commands;
683 } else {
684 0 foreach my $command (@commands) {
685 0 my $output = `$command 2>&1`;
686 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
687 0 $self->{error} = 25;
688 0 $self->{errorString} = 'Flush failed... command "' . $command . '" resulted in... ' . $output;
689 0 $self->warn;
690 }
691 }
692 }
693  
694 0 $self->{banned} = {};
695 } ## end sub flush
696  
697 =head1 ERROR CODES / FLAGS
698  
699 Error handling is provided by L<Error::Helper>. All
700 errors are considered fatal.
701  
702 =head2 1, notInited
703  
704 The backend has not been inited yet.
705  
706 =head2 8, optionsNotHash
707  
708 The item passed to new for options is not a hash.
709  
710 =head2 9, noBanItem
711  
712 No IP specified to ban or unban.
713  
714 =head2 10, banItemNotIP
715  
716 The item to ban is not an IP. Either wrong ref type or regexp
717 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
718  
719 =head2 12, backendInitError
720  
721 Failed to init the backend.
722  
723 =head2 13, banFailed
724  
725 Failed to ban the item.
726  
727 =head2 14, unbanFailed
728  
729 Failed to unban the item.
730  
731 =head2 15, listFailed
732  
733 Failed to get a list of bans.
734  
735 =head2 16, reInitFailed
736  
737 Failed to re_init the backend.
738  
739 =head2 17, teardownFailed
740  
741 Failed to teardown the backend.
742  
743 =head2 18, alreadyInited
744  
745 init called, but the backend has already been inited.
746  
747 =head2 20, modeInvalid
748  
749 The option mode is not 'src' or 'dst'.
750  
751 =head2 24, checkFailed
752  
753 The backend check raised an error.
754  
755 =head2 25, flushFailed
756  
757 Failed to flush the bans.
758  
759 =head2 30, interfacesInvalid
760  
761 The interfaces option is either undef, not an array ref, or an empty array
762 ref. It is required and must be a non-empty array ref.
763  
764 =head2 31, banCidrFailed
765  
766 Failed to ban the CIDR range.
767  
768 =head2 32, unbanCidrFailed
769  
770 Failed to unban the CIDR range.
771  
772 =head2 33, cidrItemNotCidr
773  
774 The item to ban is not a CIDR range. Either wrong ref type or it is not an
775 IPv4 or IPv6 address followed by a prefix length valid for its family.
776  
777 =head2 34, cidrNotSupported
778  
779 The backend does not support CIDR bans.
780  
781 =head2 35, listCidrFailed
782  
783 Failed to get a list of CIDR bans.
784  
785 =head1 AUTHOR
786  
787 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
788  
789 =head1 BUGS
790  
791 Please report any bugs or feature requests to C<bug-net-firewall-blockerhelper at rt.cpan.org>, or through
792 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Firewall-BlockerHelper>. I will be notified, and then you'll
793 automatically be notified of progress on your bug as I make changes.
794  
795  
796  
797  
798 =head1 SUPPORT
799  
800 You can find documentation for this module with the perldoc command.
801  
802 perldoc Net::Firewall::BlockerHelper
803  
804  
805 You can also look for information at:
806  
807 =over 4
808  
809 =item * RT: CPAN's request tracker (report bugs here)
810  
811 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Firewall-BlockerHelper>
812  
813 =item * Search CPAN
814  
815 L<https://metacpan.org/release/Net-Firewall-BlockerHelper>
816  
817 =back
818  
819  
820 =head1 ACKNOWLEDGEMENTS
821  
822  
823 =head1 LICENSE AND COPYRIGHT
824  
825 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
826  
827 This is free software, licensed under:
828  
829 The GNU Lesser General Public License, Version 2.1, February 1999
830  
831  
832 =cut
833  
834 1 1; # End of Net::Firewall::BlockerHelper
« BlockerHelper/backends/npf.pm ↑ Summary BlockerHelper/backends/azure.pm »