BlockerHelper/backends/nftables.pm

filter: Statementstmt 5.4% 19 / 350 Branchbran 0.0% 0 / 132 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 28.5% 6 / 21 Podpod 100% 13 / 13 totaltotal 6.5% 38 / 584 CC 95 SCAR 60.6
CC
95
Cov
4%
new
1190.0
_rule_specs
210.0
ban
210.0
CRAP 428.4
« BlockerHelper/backends/ipfw.pm ↑ Summary BlockerHelper/backends/ufw.pm »
1 package Net::Firewall::BlockerHelper::backends::nftables;
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 qw( Error::Helper Net::Firewall::BlockerHelper::Util );
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::nftables - nftables 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;
25  
26 my $fw_helper = Net::Firewall::BlockerHelper->new(
27 backend => 'nftables',
28 ports => ['22'],
29 protocols => ['tcp'],
30 name => 'ssh',
31 );
32  
33 $fw_helper->init_backend;
34 $fw_helper->ban(ban => '1.2.3.4');
35 $fw_helper->ban(ban => 'dead::1');
36 $fw_helper->unban(ban => '1.2.3.4');
37 $fw_helper->teardown;
38  
39 =head1 DESCRIPTION
40  
41 Blocks IPs using L<nft(8)>.
42  
43 A dedicated table C<< inet <prefix>_<name> >> is created per instance,
44 containing a base chain hooked to input, one set for IPv4
45 (C<< <prefix>_<name>_4 >>) and one for IPv6 (C<< <prefix>_<name>_6 >>), and
46 the block rules referencing them. Banning an IP adds it to the relevant set.
47 As the whole setup lives in its own table, teardown is simply deleting the
48 table.
49  
50 Requires C<nft> to be installed and in the C<PATH> of the process, which must
51 have sufficient privileges to run it.
52  
53 =head1 METHODS
54  
55 =head2 new
56  
57 Initiates the object.
58  
59 - options :: Backend specific options that will be passed to the backend unchecked
60 outside of making sure it is a hash ref if defined. See below for furhter info.
61 - Default :: {}
62  
63 - ports :: A array of ports to block. Checked to make sure they are ints within the
64 range 1 to 65535 or a valid service name via getservbyname. All ports will be
65 blocked if non are specified. Duplicates are removed.
66 - Default :: []
67  
68 - protocols :: A array of protocols to block. This is checked against
69 /etc/protocols via the function getprotobyname. Duplicates will be
70 discarded. If no protocols are given, all IPv4 and IPv6 traffic
71 sourced from the sets is blocked, unless ports are given, in which
72 case it defaults to tcp and udp. Ports are only attached to
73 port-capable protocols (tcp/udp/sctp); other protocols are blocked
74 without a port.
75 - Default :: [], or ['tcp','udp'] when ports are given
76  
77 - prefix :: Prefix to use. Must match the regex /^[a-zA-Z0-9]+$/
78 - default :: kur
79  
80 - name :: Name of this specific instance. This must be specified.
81 - default :: undef
82  
83 The options hash accepts the following.
84  
85 - type :: The drop method to use. 'drop' silently drops. 'reject' sends
86 the family-appropriate ICMP port-unreachable back.
87 - Default :: drop
88  
89 - priority :: Priority of the base chain. See nft(8).
90 - Default :: -1
91  
92 - kill :: Use conntrack(8) to drop existing connection tracking entries
93 for the banned IP, for both IPv4 and IPv6. When protocols are
94 configured the kill is scoped to them via -p, so blocking only
95 udp will not drop tcp entries and vice versa; protocols conntrack
96 can not filter by are skipped. With no protocols configured
97 everything is being blocked, so entries of every protocol are
98 dropped.
99 - Default :: 0
100  
101 All errors are considered fatal, meaning if new fails it will die.
102  
103 =cut
104  
105 0 ▶sub new {
Subroutine new: not called CC=34 SCAR=70.8
Pod documented
106 0 my ( $blank, %opts ) = @_;
107  
108 0 my $self = {
109 perror => undef,
110 error => undef,
111 errorLine => undef,
112 errorFilename => undef,
113 errorString => "",
114 errorExtra => {
115 all_errors_fatal => 1,
116 # all_fatal is what Error::Helper 2.1.0 actually checks; all_errors_fatal
117 # is kept for the name documented in its POD
118 all_fatal => 1,
119 flags => {
120 1 => 'notInited',
121 2 => 'invalidPortSpecified',
122 3 => 'portsNotArray',
123 4 => 'protocolsNotArray',
124 5 => 'invalidPortSpecified',
125 6 => 'invalidPrefixSpecified',
126 7 => 'invalidName',
127 8 => 'optionsNotHash',
128 9 => 'noBanItem',
129 10 => 'banItemNotIP',
130 12 => 'backendInitError',
131 13 => 'banFailed',
132 14 => 'unbanFailed',
133 15 => 'listFailed',
134 16 => 'reInitFailed',
135 17 => 'teardownFailed',
136 18 => 'alreadyInited',
137 19 => 'priorityInvalid',
138 20 => 'typeInvalid',
139 23 => 'initFailed',
140 24 => 'checkFailed',
141 25 => 'flushFailed',
142 26 => 'banCidrFailed',
143 27 => 'unbanCidrFailed',
144 28 => 'cidrItemNotCidr',
145 29 => 'cidrNotSupported',
146 30 => 'listCidrFailed',
147 },
148 fatal_flags => {},
149 perror_not_fatal => 0,
150 },
151 options => {
152 type => 'drop',
153 priority => -1,
154 kill => 0,
155 },
156 ports => [],
157 protocols => [],
158 testing => undef,
159 test_data => undef,
160 prefix => 'kur',
161 frontend_obj => undef,
162 inited => 0,
163 banned => {},
164 cidr_supported => 0,
165 };
166 0 bless $self;
167  
168 0 ▶ if ( defined( $opts{ports} ) && ref( $opts{ports} ) ne 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'ports'} and ref $opts{'ports'} ne 'ARRAY') { } 0 0 0
elsif (defined $opts{'ports'}) { } 0 0 0
Condition0%0/3
defined $opts{"ports"} and ref $opts{"ports"} ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"ports"} and ref $opts{"ports"} ne "ARRAY"
defined $opts{"ports"} ref $opts{"ports"} ne "ARRAY"
Truth table0%0/3
A and B
A= defined $opts{"ports"}B= ref $opts{"ports"} ne "ARRAY"
ABresult
0X0
100
111
169 0 $self->{perror} = 1;
170 0 $self->{error} = 3;
171 0 $self->{errorString} = 'ports is defined and type is not array but "' . ref( $opts{ports} ) . '"';
172 0 $self->warn;
173 } elsif ( defined( $opts{ports} ) ) {
174 0 my %ports;
175 0 foreach my $item ( @{ $opts{ports} } ) {
176 0 ▶ if ( $item =~ /^[0-9]+$/ && $item >= 1 && $item <= 65535 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($item =~ /^[0-9]+$/ and $item >= 1 and $item <= 65535) { } 0 0 0
elsif ($item =~ /^[0-9]+$/) { } 0 0 0
Condition0%0/6
$item =~ /^[0-9]+$/ and $item >= 1 and $item <= 65535
!ll&&!rl&&r
000
$item =~ /^[0-9]+$/ and $item >= 1
!ll&&!rl&&r
000
MC/DC0%0/3
$item =~ /^[0-9]+$/ and $item >= 1 and $item <= 65535
$item =~ /^[0-9]+$/ $item >= 1 $item <= 65535
Truth table0%0/4
A and B and C
A= $item =~ /^[0-9]+$/B= $item >= 1C= $item <= 65535
ABCresult
0XX0
10X0
1100
1111
177 0 $ports{$item} = 1;
178 } elsif ( $item =~ /^[0-9]+$/ ) {
179 0 $self->{perror} = 1;
180 0 $self->{error} = 2;
181 $self->{errorString}
182 0 = $item . ' is not a valid value for a port as it must be a int within the range 1 to 65535';
183 0 $self->warn;
184 } else {
185 # just using tcp here as protocol must be specified
186 0 my ( $name, $aliases, $port, $proto ) = getservbyname( $item, 'tcp' );
187 0 ▶ if ( !defined($port) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $port) 0 0 0
188 0 $self->{perror} = 1;
189 0 $self->{error} = 2;
190 $self->{errorString}
191 0 = $item . ' could not be resolved to a port name via getservbyname("' . $item . '", "tcp")';
192 0 $self->warn;
193 }
194 0 $ports{$port} = 1;
195 } ## end else [ if ( $item =~ /^[0-9]+$/ && $item >= 1 &&...)]
196 } ## end foreach my $item ( @{ $opts{ports} } )
197 0 my @port_keys = keys(%ports);
198 0 @port_keys = sort { $a <=> $b } @port_keys;
199 0 push( @{ $self->{ports} }, @port_keys );
200 } ## end elsif ( defined( $opts{ports} ) )
201  
202 0 ▶ if ( defined( $opts{protocols} ) && ref( $opts{protocols} ) ne 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'protocols'} and ref $opts{'protocols'} ne 'ARRAY') { } 0 0 0
elsif (defined $opts{'protocols'}) { } 0 0 0
Condition0%0/3
defined $opts{"protocols"} and ref $opts{"protocols"} ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"protocols"} and ref $opts{"protocols"} ne "ARRAY"
defined $opts{"protocols"} ref $opts{"protocols"} ne "ARRAY"
Truth table0%0/3
A and B
A= defined $opts{"protocols"}B= ref $opts{"protocols"} ne "ARRAY"
ABresult
0X0
100
111
203 0 $self->{perror} = 1;
204 0 $self->{error} = 4;
205 0 $self->{errorString} = 'protocols is defined and type is not array but "' . ref( $opts{protocols} ) . '"';
206 0 $self->warn;
207 } elsif ( defined( $opts{protocols} ) ) {
208 0 my %protocols;
209 0 foreach my $item ( @{ $opts{protocols} } ) {
210 0 my ( $name, $aliases, $proto ) = getprotobyname($item);
211 # if this is undef, it means it is not a known protocol
212 0 ▶ if ( !defined($proto) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $proto) 0 0 0
213 0 $self->{perror} = 1;
214 0 $self->{error} = 5;
215 $self->{errorString}
216 0 = $item . ' could not be resolved to a protocol via getprotobyname("' . $item . '")';
217 0 $self->warn;
218 }
219 0 $protocols{$item} = 1;
220 } ## end foreach my $item ( @{ $opts{protocols} } )
221 0 my @protocols_keys = keys(%protocols);
222 0 @protocols_keys = sort { $a cmp $b } @protocols_keys;
223 0 push( @{ $self->{protocols} }, @protocols_keys );
224 } ## end elsif ( defined( $opts{protocols} ) )
225  
226 # make sure prefix is sane if defiend
227 0 ▶ 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 0 0
elsif (defined $opts{'prefix'}) { } 0 0 0
Condition0%0/3
defined $opts{"prefix"} and not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
!ll&&!rl&&r
000
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 table0%0/3
A and not(B)
A= defined $opts{"prefix"}B= not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
ABresult
0X0
100
111
228 0 $self->{perror} = 1;
229 0 $self->{error} = 6;
230 $self->{errorString}
231 0 = '"' . $opts{prefix} . '" is not a valid prefix as it does not match the regex /^[a-zA-Z0-9]+$/';
232 0 $self->warn;
233 } elsif ( defined( $opts{prefix} ) ) {
234 0 $self->{prefix} = $opts{prefix};
235 }
236  
237 # make sure we have a name and that it is valid
238 0 ▶ if ( !defined( $opts{name} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'name'}) { } 0 0 0
elsif (not $opts{'name'} =~ /^[a-zA-Z0-9\-]+$/) { } 0 0 0
239 0 $self->{perror} = 1;
240 0 $self->{error} = 7;
241 0 $self->{errorString} = 'name is undef';
242 0 $self->warn;
243 } elsif ( $opts{name} !~ /^[a-zA-Z0-9\-]+$/ ) {
244 0 $self->{perror} = 1;
245 0 $self->{error} = 7;
246 0 $self->{errorString} = 'name set to "' . $opts{name} . '" which does not match the regexp /^[a-zA-Z0-9\-]+$/';
247 0 $self->warn;
248 }
249 0 $self->{name} = $opts{name};
250  
251 # used internally for testing
252 0 ▶ if ( defined( $opts{testing} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"testing"}) 0 0 0
253 0 $self->{testing} = $opts{testing};
254 }
255 0 ▶ if ( defined( $opts{frontend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"frontend_obj"}) 0 0 0
256 0 $self->{frontend_obj} = $opts{frontend_obj};
257 }
258  
259 0 ▶ if ( defined( $opts{options} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"options"}) 0 0 0
260 0 ▶ if ( ref( $opts{options} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"options"} ne "HASH") 0 0 0
261 0 $self->{perror} = 1;
262 0 $self->{error} = 8;
263 0 $self->{errorString} = 'ref for options is "' . ref( $opts{options} ) . '" and not HASH';
264 0 $self->warn;
265 }
266 0 $self->{options} = $opts{options};
267  
268 0 ▶ 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 0 0
elsif (defined $opts{'options'}{'type'} and $opts{'options'}{'type'} ne 'drop' and $opts{'options'}{'type'} ne 'reject') { } 0 0 0
elsif (not defined $opts{'options'}{'type'}) { } 0 0 0
Condition0%0/3
defined $opts{"options"}{"type"} and ref $opts{"options"}{"type"} ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"options"}{"type"} and ref $opts{"options"}{"type"} ne ""
defined $opts{"options"}{"type"} ref $opts{"options"}{"type"} ne ""
Truth table0%0/3
A and B
A= defined $opts{"options"}{"type"}B= ref $opts{"options"}{"type"} ne ""
ABresult
0X0
100
111
269 0 $self->{perror} = 1;
270 0 $self->{error} = 20;
271 0 $self->{errorString} = 'ref for $opts{options}{type} is "' . ref( $opts{options}{type} ) . '" and not ""';
272 0 $self->warn;
273 ▶ } elsif ( defined( $opts{options}{type} )
Condition0%0/6
defined $opts{"options"}{"type"} and $opts{"options"}{"type"} ne "drop" and $opts{"options"}{"type"} ne "reject"
!ll&&!rl&&r
000
defined $opts{"options"}{"type"} and $opts{"options"}{"type"} ne "drop"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $opts{"options"}{"type"} and $opts{"options"}{"type"} ne "drop" and $opts{"options"}{"type"} ne "reject"
defined $opts{"options"}{"type"} $opts{"options"}{"type"} ne "drop" $opts{"options"}{"type"} ne "reject"
Truth table0%0/4
A and B and C
A= defined $opts{"options"}{"type"}B= $opts{"options"}{"type"} ne "drop"C= $opts{"options"}{"type"} ne "reject"
ABCresult
0XX0
10X0
1100
1111
274 && $opts{options}{type} ne 'drop'
275 && $opts{options}{type} ne 'reject' )
276 {
277 0 $self->{perror} = 1;
278 0 $self->{error} = 20;
279 $self->{errorString}
280 0 = '$opts{options}{type} is "' . $opts{options}{type} . '" and not "drop" or "reject"';
281 0 $self->warn;
282 } elsif ( !defined( $opts{options}{type} ) ) {
283 0 $self->{options}{type} = 'drop';
284 }
285  
286 0 ▶ if ( !defined( $opts{options}{kill} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"options"}{"kill"}) 0 0 0
287 0 $self->{options}{kill} = 0;
288 }
289  
290 0 ▶ if ( defined( $opts{options}{priority} ) && $opts{options}{priority} !~ /^\-?[0-9]+$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'options'}{'priority'} and not $opts{'options'}{'priority'} =~ /^\-?[0-9]+$/) { } 0 0 0
elsif (not defined $opts{'options'}{'priority'}) { } 0 0 0
Condition0%0/3
defined $opts{"options"}{"priority"} and not $opts{"options"}{"priority"} =~ /^\-?[0-9]+$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"options"}{"priority"} and not $opts{"options"}{"priority"} =~ /^\-?[0-9]+$/
defined $opts{"options"}{"priority"} not $opts{"options"}{"priority"} =~ /^\-?[0-9]+$/
Truth table0%0/3
A and not(B)
A= defined $opts{"options"}{"priority"}B= not $opts{"options"}{"priority"} =~ /^\-?[0-9]+$/
ABresult
0X0
100
111
291 0 $self->{perror} = 1;
292 0 $self->{error} = 19;
293 $self->{errorString}
294 0 = '$opts{options}{priority} is "' . $opts{options}{priority} . '" and not an int';
295 0 $self->warn;
296 } elsif ( !defined( $opts{options}{priority} ) ) {
297 0 $self->{options}{priority} = -1;
298 }
299 } ## end if ( defined( $opts{options} ) )
300  
301 0 return $self;
302 } ## end sub new
303  
304 # Internal helper. Returns the three nftables object names this instance owns:
305 # its table, which is also the name of the chain inside it, and its two sets.
306 #
307 # All three are derived from the prefix and the name rather than stored, so
308 # every part of the backend agrees on what to create, reference, and tear down
309 # without threading the names around. The prefix and name pair is unique per
310 # instance, so two instances can share a host without colliding.
311 #
312 # The table and the chain deliberately share one name. nftables scopes chain
313 # names to their table, so there is no collision, and having the instance own
314 # a whole inet table means teardown can delete the table and take the chain,
315 # both sets, and every rule with it in one command.
316 #
317 # There are still two sets despite the table being inet and therefore holding
318 # both families, because an nftables set is typed: one is ipv4_addr and the
319 # other ipv6_addr, and neither will hold the other's addresses.
320 #
321 # Takes no arguments.
322 #
323 # Returns a three element list of the table name, the IPv4 set name, and the
324 # IPv6 set name, in that order. Note this differs from the iptables backend's
325 # _set_names, which returns the sets first and the chain last.
326 #
327 # my ( $table, $set4, $set6 ) = $self->_names;
328 #
329 # # with prefix "kur" and name "ssh"
330 # # $table is 'kur_ssh' -- and the chain inside it is also 'kur_ssh'
331 # # $set4 is 'kur_ssh_4'
332 # # $set6 is 'kur_ssh_6'
333 0 ▶sub _names {
Subroutine _names: not called CC=1 SCAR=6.9
334 0 my ($self) = @_;
335  
336 0 my $table = $self->{prefix} . '_' . $self->{name};
337 0 return ( $table, $table . '_4', $table . '_6' );
338 }
339  
340 # Internal helper. Works out the full cross product of family, protocol, and
341 # ports this instance needs to block and returns it as nftables rule
342 # statements, along with a record of which sets those statements actually
343 # mention. Essentially all of the backend's rule logic lives here.
344 #
345 # The second return value exists for check. Verifying the setup is still
346 # intact means confirming the sets the rules depend on are present, and which
347 # sets those are is not always both of them: a configuration whose protocols
348 # are all one family, say icmp only, produces rules referencing just the IPv4
349 # set, and demanding the IPv6 set exist would then report a healthy setup as
350 # broken. Rather than have check re-derive that, it is recorded here where the
351 # decision is actually made.
352 #
353 # Protocols belonging to the wrong family are skipped, so icmp never lands in
354 # an ip6 rule and the various spellings of IPv6 ICMP never land in an ip one.
355 # Ports are only attached to protocols that have them; anything else is
356 # matched with meta l4proto, which is how nftables matches a protocol it has
357 # no dedicated keyword for.
358 #
359 # Unlike the iptables backend there is no tarpit or delude here, so the target
360 # is just drop or reject and does not vary by family.
361 #
362 # Takes no arguments; family, protocols, ports, and type all come from the
363 # object.
364 #
365 # Returns a two element list:
366 #
367 # - an arrayref of rule statement strings, each the complete body of one
368 # nft rule, ready to have "nft add rule inet <table> <chain> " put in
369 # front of it
370 # - a hashref keyed by the set names those statements reference, values
371 # all 1, for check to confirm
372 #
373 # my ( $specs, $referenced ) = $self->_rule_specs;
374 #
375 # # no protocols and no ports, type drop
376 # # $specs is [ 'ip saddr @kur_ssh_4 drop', 'ip6 saddr @kur_ssh_6 drop' ]
377 # # $referenced is { kur_ssh_4 => 1, kur_ssh_6 => 1 }
378 #
379 # # ports 22 with no protocols: defaults to tcp and udp per family
380 # # 'tcp dport { 22 } ip saddr @kur_ssh_4 drop'
381 # # 'udp dport { 22 } ip saddr @kur_ssh_4 drop'
382 # # 'tcp dport { 22 } ip6 saddr @kur_ssh_6 drop'
383 # # 'udp dport { 22 } ip6 saddr @kur_ssh_6 drop'
384 #
385 # # protocols gre, which takes no ports
386 # # 'meta l4proto gre ip saddr @kur_ssh_4 drop'
387 # # 'meta l4proto gre ip6 saddr @kur_ssh_6 drop'
388 0 ▶sub _rule_specs {
Subroutine _rule_specs: not called CC=14 SCAR=53.5
389 0 my ($self) = @_;
390  
391 0 my ( $table, $set4, $set6 ) = $self->_names;
392  
393 0 my @ports = @{ $self->{ports} };
394 0 my @protos = @{ $self->{protocols} };
395 0 my $port_str = join( ', ', @ports );
396  
397 0 ▶ my $target = $self->{options}{type} eq 'reject' ? 'reject' : 'drop';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'options'}{'type'} eq 'reject' ? : 0 0 0
398  
399 # protocols that accept a port specification
400 0 my %port_ok = ( tcp => 1, udp => 1, sctp => 1 );
401  
402 # the various names the IPv6 ICMP protocol may go by
403 0 my %v6_icmp = ( 'ipv6-icmp' => 1, 'icmp6' => 1, 'icmpv6' => 1 );
404  
405 0 my @families = (
406 { set => $set4, saddr => 'ip', family => 4 },
407 { set => $set6, saddr => 'ip6', family => 6 },
408 );
409  
410 0 my @specs;
411 my %referenced;
412 0 foreach my $fam (@families) {
413 0 my $match = $fam->{saddr} . ' saddr @' . $fam->{set};
414  
415 0 ▶ if ( !@protos && !@ports ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @protos || @ports) { } 0 0 0
elsif (not @protos and @ports) { } 0 0 0
Condition0%0/3
@protos || @ports
l!l&&r!l&&!r
000
MC/DC0%0/2
@protos || @ports
@protos @ports
Truth table0%0/3
A || B
A= @protosB= @ports
ABresult
1X1
011
000
416 # block everything sourced from the set
417 0 push( @specs, $match . ' ' . $target );
418 0 $referenced{ $fam->{set} } = 1;
419 ▶ } elsif ( !@protos && @ports ) {
Condition0%0/3
not @protos and @ports
!ll&&!rl&&r
000
MC/DC0%0/2
not @protos and @ports
not @protos @ports
Truth table0%0/3
not(A) and B
A= not @protosB= @ports
ABresult
0X0
100
111
420 # ports require a protocol, default to tcp and udp
421 0 foreach my $proto ( 'tcp', 'udp' ) {
422 0 push( @specs, $proto . ' dport { ' . $port_str . ' } ' . $match . ' ' . $target );
423 0 $referenced{ $fam->{set} } = 1;
424 }
425 } else {
426 0 foreach my $proto (@protos) {
427 # skip protocols that do not belong to this family
428 0 ▶ if ( $fam->{family} == 6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fam->{'family'} == 6) { } 0 0 0
429 0 ▶ next if ( $proto eq 'icmp' );
Branch: 1 branch
BranchTrueFalseTotal
if $proto eq "icmp" 0 0 0
430 } else {
431 0 ▶ next if ( $v6_icmp{$proto} );
Branch: 1 branch
BranchTrueFalseTotal
if $v6_icmp{$proto} 0 0 0
432 }
433  
434 0 ▶ if ( @ports && $port_ok{$proto} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ports and $port_ok{$proto}) { } 0 0 0
Condition0%0/3
@ports and $port_ok{$proto}
!ll&&!rl&&r
000
MC/DC0%0/2
@ports and $port_ok{$proto}
@ports $port_ok{$proto}
Truth table0%0/3
A and B
A= @portsB= $port_ok{$proto}
ABresult
0X0
100
111
435 0 push( @specs, $proto . ' dport { ' . $port_str . ' } ' . $match . ' ' . $target );
436 } else {
437 0 push( @specs, 'meta l4proto ' . $proto . ' ' . $match . ' ' . $target );
438 }
439 0 $referenced{ $fam->{set} } = 1;
440 } ## end foreach my $proto (@protos)
441 } ## end else [ if ( !@protos && !@ports ) ]
442 } ## end foreach my $fam (@families)
443  
444 0 return ( \@specs, \%referenced );
445 } ## end sub _rule_specs
446  
447 =head2 init
448  
449 Initiates the backend. First a best effort C<nft delete table> is run to
450 clear any stale copy of the table, then the table
451 C<< inet <prefix>_<name> >>, a base chain of the same name hooked to input
452 at the configured priority, the IPv4 set (C<< <prefix>_<name>_4 >>), the
453 IPv6 set (C<< <prefix>_<name>_6 >>), and the block rules referencing the
454 sets are created via C<nft add>. A failure of any of the creation commands
455 raises an error.
456  
457 $backend->init;
458  
459 =cut
460  
461 0 ▶sub init {
Subroutine init: not called CC=9 SCAR=45.0
Pod documented
462 0 my ( $self, %opts ) = @_;
463  
464 0 $self->errorblank;
465  
466 0 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 0 0
467 0 $self->{error} = 18;
468 0 $self->{errorString} = 'backend has already been inited';
469 0 $self->warn;
470 }
471  
472 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
473 0 $self->{frontend_obj}->{test_data} = {};
474 }
475  
476 0 my ( $table, $set4, $set6 ) = $self->_names;
477  
478 # stale state cleanup, allowed to fail
479 0 my @fail_okay_commands = ( "nft 'delete table inet " . $table . "'" );
480  
481 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
482 0 $self->{frontend_obj}->{test_data}{fail_okay_commands} = \@fail_okay_commands;
483 } else {
484 0 foreach my $item (@fail_okay_commands) {
485 0 my $output = `$item 2>&1`;
486 }
487 }
488  
489 0 my ($specs) = $self->_rule_specs;
490  
491 0 my @commands;
492 0 push( @commands, "nft 'add table inet " . $table . "'" );
493 push( @commands,
494 "nft 'add chain inet "
495 . $table . ' '
496 . $table
497 . ' { type filter hook input priority '
498 . $self->{options}{priority}
499 0 . " ; policy accept ; }'" );
500 0 push( @commands, "nft 'add set inet " . $table . ' ' . $set4 . " { type ipv4_addr; }'" );
501 0 push( @commands, "nft 'add set inet " . $table . ' ' . $set6 . " { type ipv6_addr; }'" );
502 0 foreach my $spec ( @{$specs} ) {
503 0 push( @commands, "nft 'add rule inet " . $table . ' ' . $table . ' ' . $spec . "'" );
504 }
505  
506 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
507 0 $self->{frontend_obj}->{test_data}{commands} = \@commands;
508 } else {
509 0 foreach my $item (@commands) {
510 0 my $output = `$item 2>&1`;
511 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
512 0 $self->{error} = 23;
513 $self->{errorString}
514 0 = 'init failed. non-zero exit code for the command... "' . $item . '"... output... ' . $output;
515 0 $self->warn;
516 }
517 }
518 } ## end else [ if ( $self->{testing} ) ]
519  
520 0 $self->{inited} = 1;
521 } ## end sub init
522  
523 =head2 ban
524  
525 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address
526 and lowercased, then added to the family-appropriate set via
527 C<nft add element>. If the C<kill> option is set, conntrack(8) is then used
528 to drop existing connection tracking entries for the IP. Banning an already
529 banned IP is a noop.
530  
531 $backend->ban(ban => $ip);
532  
533 =cut
534  
535 0 ▶sub ban {
Subroutine ban: not called CC=14 SCAR=53.5
Pod documented
536 0 my ( $self, %opts ) = @_;
537  
538 0 $self->errorblank;
539  
540 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
541 0 $self->{error} = 1;
542 0 $self->{errorString} = 'backend has not been inited';
543 0 $self->warn;
544 0 return;
545 }
546  
547 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::nftables::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv6_re\z/) { } 0 0 0
548 0 $self->{error} = 9;
549 0 $self->{errorString} = 'Nothing specified for the value ban';
550 0 $self->warn;
551 0 return;
552 } elsif ( ref( $opts{ban} ) ne '' ) {
553 0 $self->{error} = 10;
554 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
555 0 $self->warn;
556 0 return;
557 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition0%0/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv6_re\z/
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv6_re\z/
Truth table0%0/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv6_re\z/
ABresult
1X1
011
000
558 && $opts{ban} !~ /\A$IPv6_re\z/ )
559 {
560 0 $self->{error} = 10;
561 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
562 0 $self->warn;
563 0 return;
564 }
565  
566 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
567 0 $opts{ban} = lc( $opts{ban} );
568  
569 0 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 0 0
570 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
571 0 $self->{frontend_obj}->{test_data} = 'already banned';
572 }
573 0 return;
574 }
575  
576 0 my ( $table, $set4, $set6 ) = $self->_names;
577 0 ▶ my $set = ( $opts{ban} =~ /\A$IPv4_re\z/ ) ? $set4 : $set6;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv4_re\z/ ? : 0 0 0
578  
579 0 my $command = "nft 'add element inet " . $table . ' ' . $set . ' { ' . $opts{ban} . " }'";
580  
581 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
582 0 $self->{frontend_obj}->{test_data} = [$command];
583 } else {
584 0 my $output = `$command 2>&1`;
585 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
586 0 $self->{error} = 13;
587 $self->{errorString}
588 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
589 0 $self->warn;
590 }
591 }
592  
593 0 ▶ if ( $self->{options}{kill} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"kill"}) 0 0 0
594 0 foreach my $kill_command ( $self->_kill_commands( $opts{ban} ) ) {
595 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
596 0 push( @{ $self->{frontend_obj}->{test_data} }, $kill_command );
597 } else {
598 # conntrack returns non-zero when there is nothing to delete,
599 # so its exit code is intentionally ignored
600 0 my $output = `$kill_command 2>&1`;
601 }
602 }
603 } ## end if ( $self->{options}{kill} )
604  
605 0 $self->{banned}{ $opts{ban} } = 1;
606 } ## end sub ban
607  
608 =head2 unban
609  
610 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
611 and lowercased, then removed from the family-appropriate set via
612 C<nft delete element>. Unbanning an IP that is not banned is a noop.
613  
614 $backend->unban(ban => $ip);
615  
616 =cut
617  
618 0 ▶sub unban {
Subroutine unban: not called CC=11 SCAR=48.8
Pod documented
619 0 my ( $self, %opts ) = @_;
620  
621 0 $self->errorblank;
622  
623 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
624 0 $self->{error} = 1;
625 0 $self->{errorString} = 'backend has not been inited';
626 0 $self->warn;
627 0 return;
628 }
629  
630 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::nftables::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv6_re\z/) { } 0 0 0
631 0 $self->{error} = 9;
632 0 $self->{errorString} = 'Nothing specified for the value ban';
633 0 $self->warn;
634 0 return;
635 } elsif ( ref( $opts{ban} ) ne '' ) {
636 0 $self->{error} = 10;
637 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
638 0 $self->warn;
639 0 return;
640 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition0%0/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv6_re\z/
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv6_re\z/
Truth table0%0/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv6_re\z/
ABresult
1X1
011
000
641 && $opts{ban} !~ /\A$IPv6_re\z/ )
642 {
643 0 $self->{error} = 10;
644 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
645 0 $self->warn;
646 0 return;
647 }
648  
649 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
650 0 $opts{ban} = lc( $opts{ban} );
651  
652 0 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 0 0
653 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
654 0 $self->{frontend_obj}->{test_data} = 'not banned';
655 }
656 0 return;
657 }
658  
659 0 my ( $table, $set4, $set6 ) = $self->_names;
660 0 ▶ my $set = ( $opts{ban} =~ /\A$IPv4_re\z/ ) ? $set4 : $set6;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv4_re\z/ ? : 0 0 0
661  
662 0 my $command = "nft 'delete element inet " . $table . ' ' . $set . ' { ' . $opts{ban} . " }'";
663  
664 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
665 0 $self->{frontend_obj}->{test_data} = $command;
666 } else {
667 0 my $output = `$command 2>&1`;
668 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
669 0 $self->{error} = 14;
670 $self->{errorString}
671 0 = 'unban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
672 0 $self->warn;
673 }
674 }
675  
676 0 delete( $self->{banned}{ $opts{ban} } );
677 } ## end sub unban
678  
679 =head2 list
680  
681 List banned IPs. Returns an array of the currently banned IPs from the
682 retained in-memory state; the sets are not queried.
683  
684 my @banned = $backend->list;
685  
686 =cut
687  
688 0 ▶sub list {
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
689 0 my ( $self, %opts ) = @_;
690  
691 0 $self->errorblank;
692  
693 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
694 0 $self->{frontend_obj}->{test_data} = 'list';
695 }
696  
697 0 return keys( %{ $self->{banned} } );
698 }
699  
700 =head2 re_init
701  
702 Tears down and re-initiates the backend, recreating the table, chain, sets,
703 and rules, then re-adds every previously banned IP to the relevant set via
704 C<nft add element>. The teardown is best effort as a partially or fully
705 wiped setup is exactly what this needs to recover from.
706  
707 $backend->re_init;
708  
709 =cut
710  
711 0 ▶sub re_init {
Subroutine re_init: not called CC=6 SCAR=37.4
Pod documented
712 0 my ( $self, %opts ) = @_;
713  
714 0 $self->errorblank;
715  
716 # teardown is best effort here as a partially or fully wiped setup is
717 # exactly what re_init needs to recover from; init cleans up any remnants
718 {
719 0 local $@;
720 0 eval { $self->teardown; };
721 }
722 0 $self->init;
723  
724 0 my ( $table, $set4, $set6 ) = $self->_names;
725  
726 0 my @to_ban = keys( %{ $self->{banned} } );
727  
728 0 my @re_init_test_data;
729 0 foreach my $item (@to_ban) {
730 0 ▶ my $set = ( $item =~ /\A$IPv4_re\z/ ) ? $set4 : $set6;
Branch: 1 branch
BranchTrueFalseTotal
$item =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv4_re\z/ ? : 0 0 0
731 0 my $command = "nft 'add element inet " . $table . ' ' . $set . ' { ' . $item . " }'";
732  
733 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
734 0 push( @re_init_test_data, $command );
735 } else {
736 0 my $output = `$command 2>&1`;
737 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
738 0 $self->{error} = 13;
739 $self->{errorString}
740 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
741 0 $self->warn;
742 }
743 }
744 } ## end foreach my $item (@to_ban)
745  
746 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
747 0 $self->{frontend_obj}->{test_data} = \@re_init_test_data;
748 }
749  
750 0 $self->{inited} = 1;
751 } ## end sub re_init
752  
753 =head2 teardown
754  
755 Tears down the setup for the backend by deleting the table, which removes
756 the chain, sets, and rules with it.
757  
758 $backend->teardown;
759  
760 =cut
761  
762 0 ▶sub teardown {
Subroutine teardown: not called CC=4 SCAR=30.0
Pod documented
763 0 my ( $self, %opts ) = @_;
764  
765 0 $self->errorblank;
766  
767 0 $self->{inited} = 0;
768  
769 0 my ( $table, $set4, $set6 ) = $self->_names;
770  
771 0 my @commands = ( "nft 'delete table inet " . $table . "'" );
772  
773 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
774 0 $self->{frontend_obj}->{test_data} = \@commands;
775 } else {
776 0 foreach my $item (@commands) {
777 0 my $output = `$item 2>&1`;
778 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
779 0 $self->{error} = 17;
780 $self->{errorString}
781 0 = 'teardown failed. non-zero exit code for the command... "' . $item . '"... output... ' . $output;
782 0 $self->warn;
783 }
784 }
785 } ## end else [ if ( $self->{testing} ) ]
786 } ## end sub teardown
787  
788 =head2 stop
789  
790 Alias for L</teardown>, provided for parity with the fail2ban C<actionstop>
791 concept.
792  
793 $backend->stop;
794  
795 =cut
796  
797 0 ▶sub stop {
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
798 0 my ( $self, %opts ) = @_;
799  
800 0 return $self->teardown(%opts);
801 }
802  
803 =head2 check
804  
805 Verifies that the table is still present and that the chain still contains
806 rules referencing each set the block rules use. Returns a true value if the
807 setup is intact and a false value if any part of it appears to have been
808 removed. This is the equivalent of fail2ban's C<actioncheck>.
809  
810 if ( !$backend->check ) {
811 $backend->re_init;
812 }
813  
814 =cut
815  
816 0 ▶sub check {
Subroutine check: not called CC=5 SCAR=34.0
Pod documented
817 0 my ( $self, %opts ) = @_;
818  
819 0 $self->errorblank;
820  
821 0 my ( $table, $set4, $set6 ) = $self->_names;
822  
823 0 my ( $specs, $referenced ) = $self->_rule_specs;
824  
825 0 my @commands = ( "nft 'list table inet " . $table . "'" );
826 0 foreach my $set ( sort( keys( %{$referenced} ) ) ) {
827 0 push( @commands, "nft 'list chain inet " . $table . ' ' . $table . "' | grep -q '\@" . $set . "'" );
828 }
829  
830 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
831 0 $self->{frontend_obj}->{test_data} = \@commands;
832 0 return 1;
833 }
834  
835 0 foreach my $item (@commands) {
836 0 my $output = `$item 2>&1`;
837 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
838 0 return 0;
839 }
840 }
841  
842 0 return 1;
843 } ## end sub check
844  
845 =head2 flush
846  
847 Removes all currently banned IPs at once by flushing both sets, leaving the
848 table, chain, and rules in place. This is the equivalent of fail2ban's
849 C<actionflush>.
850  
851 $backend->flush;
852  
853 =cut
854  
855 0 ▶sub flush {
Subroutine flush: not called CC=5 SCAR=34.0
Pod documented
856 0 my ( $self, %opts ) = @_;
857  
858 0 $self->errorblank;
859  
860 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
861 0 $self->{error} = 1;
862 0 $self->{errorString} = 'backend has not been inited';
863 0 $self->warn;
864 0 return;
865 }
866  
867 0 my ( $table, $set4, $set6 ) = $self->_names;
868  
869 0 my @commands = (
870 "nft 'flush set inet " . $table . ' ' . $set4 . "'",
871 "nft 'flush set inet " . $table . ' ' . $set6 . "'",
872 );
873  
874 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
875 0 $self->{frontend_obj}->{test_data} = \@commands;
876 } else {
877 0 foreach my $item (@commands) {
878 0 my $output = `$item 2>&1`;
879 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
880 0 $self->{error} = 25;
881 $self->{errorString}
882 0 = 'flush failed. non-zero exit code for the command... "' . $item . '"... output... ' . $output;
883 0 $self->warn;
884 }
885 }
886 } ## end else [ if ( $self->{testing} ) ]
887  
888 0 $self->{banned} = {};
889 } ## end sub flush
890  
891 =head2 ban_cidr
892  
893 CIDR bans are not supported by this backend; this always sets the
894 cidrNotSupported error.
895  
896 $backend->ban_cidr(ban => '1.2.3.0/24');
897  
898 =cut
899  
900 0 ▶sub ban_cidr {
Subroutine ban_cidr: not called CC=1 SCAR=6.9
Pod documented
901 0 my ( $self, %opts ) = @_;
902  
903 0 $self->errorblank;
904  
905 0 $self->{error} = 29;
906 0 $self->{errorString} = 'the ' . __PACKAGE__ . ' backend does not support CIDR bans';
907 0 $self->warn;
908  
909 0 return;
910 } ## end sub ban_cidr
911  
912 =head2 unban_cidr
913  
914 CIDR bans are not supported by this backend; this always sets the
915 cidrNotSupported error.
916  
917 $backend->unban_cidr(ban => '1.2.3.0/24');
918  
919 =cut
920  
921 0 ▶sub unban_cidr {
Subroutine unban_cidr: not called CC=1 SCAR=6.9
Pod documented
922 0 my ( $self, %opts ) = @_;
923  
924 0 $self->errorblank;
925  
926 0 $self->{error} = 29;
927 0 $self->{errorString} = 'the ' . __PACKAGE__ . ' backend does not support CIDR bans';
928 0 $self->warn;
929  
930 0 return;
931 } ## end sub unban_cidr
932  
933 =head2 list_cidr
934  
935 CIDR bans are not supported by this backend, so this always returns an empty
936 list.
937  
938 my @banned_cidrs = $backend->list_cidr;
939  
940 =cut
941  
942 0 ▶sub list_cidr {
Subroutine list_cidr: not called CC=1 SCAR=6.9
Pod documented
943 0 my ( $self, %opts ) = @_;
944  
945 0 $self->errorblank;
946  
947 0 return ();
948 }
949  
950 =head1 ERROR CODES / FLAGS
951  
952 Error handling is provided by L<Error::Helper>. All
953 errors are considered fatal.
954  
955 =head2 1, notInited
956  
957 The backend has not been inited yet.
958  
959 =head2 2, invalidPortSpecified
960  
961 Port is either not an int within the range 1 to 65535 or a name that can be resolved by getservbyname.
962  
963 =head2 3, portsNotArray
964  
965 The data passed to new for ports is not an array.
966  
967 =head2 4, protocolsNotArray
968  
969 The data passed to new for protocols is not an array.
970  
971 =head2 5, invalidPortSpecified
972  
973 Port is either not an int within the range 1 to 65535 or a name that can be resolved by getservbyname.
974  
975 =head2 6, invalidPrefixSpecified
976  
977 The specified prefix did not match /^[a-zA-Z0-9]+$/.
978  
979 =head2 7, invalidName
980  
981 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
982  
983 =head2 8, optionsNotHash
984  
985 The item passed to new for options is not a hash.
986  
987 =head2 9, noBanItem
988  
989 No IP specified to ban or unban.
990  
991 =head2 10, banItemNotIP
992  
993 The item to ban is not an IP. Either wrong ref type or regexp
994 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
995  
996 =head2 12, backendInitError
997  
998 Failed to init the backend.
999  
1000 =head2 13, banFailed
1001  
1002 Failed to ban the item.
1003  
1004 =head2 14, unbanFailed
1005  
1006 Failed to unban the item.
1007  
1008 =head2 15, listFailed
1009  
1010 Failed to get a list of bans.
1011  
1012 =head2 16, reInitFailed
1013  
1014 Failed to re_init the backend.
1015  
1016 =head2 17, teardownFailed
1017  
1018 Failed to teardown the backend.
1019  
1020 =head2 18, alreadyInited
1021  
1022 init called, but the backend has already been inited.
1023  
1024 =head2 19, priorityInvalid
1025  
1026 The option priority is not an int.
1027  
1028 =head2 20, typeInvalid
1029  
1030 The option type is not "drop" or "reject".
1031  
1032 =head2 23, initFailed
1033  
1034 One of the required commands for init failed.
1035  
1036 =head2 24, checkFailed
1037  
1038 The backend check raised an error.
1039  
1040 =head2 25, flushFailed
1041  
1042 Failed to flush the bans.
1043  
1044 =head2 26, banCidrFailed
1045  
1046 Failed to ban the CIDR range.
1047  
1048 =head2 27, unbanCidrFailed
1049  
1050 Failed to unban the CIDR range.
1051  
1052 =head2 28, cidrItemNotCidr
1053  
1054 The item to ban is not a CIDR range. Either wrong ref type or it is not an
1055 IPv4 or IPv6 address followed by a prefix length valid for its family.
1056  
1057 =head2 29, cidrNotSupported
1058  
1059 The backend does not support CIDR bans.
1060  
1061 =head2 30, listCidrFailed
1062  
1063 Failed to get a list of CIDR bans.
1064  
1065 =head1 AUTHOR
1066  
1067 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
1068  
1069 =head1 BUGS
1070  
1071 Please report any bugs or feature requests to C<bug-net-firewall-blockerhelper at rt.cpan.org>, or through
1072 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Firewall-BlockerHelper>.
1073  
1074 =head1 LICENSE AND COPYRIGHT
1075  
1076 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
1077  
1078 This is free software, licensed under:
1079  
1080 The GNU Lesser General Public License, Version 2.1, February 1999
1081  
1082  
1083 =cut
1084  
1085 1 1; # End of Net::Firewall::BlockerHelper
« BlockerHelper/backends/ipfw.pm ↑ Summary BlockerHelper/backends/ufw.pm »