BlockerHelper/backends/bgp_rtbh.pm

filter: Statementstmt 49.2% 185 / 376 Branchbran 51.8% 112 / 216 Conditioncond 53.3% 16 / 30 MC/DCmcdc 20.0% 4 / 20 Subroutinesub 86.9% 20 / 23 Podpod 100% 13 / 13 totaltotal 51.6% 350 / 678 CC 127 SCAR 36.5
CC
127
Cov
50%
_route_command_cidr
103.1
re_init
90.0
flush
56.0
CRAP 38.4
« BlockerHelper/backends/abuseipdb.pm ↑ Summary BlockerHelper/backends/routeros.pm »
1 package Net::Firewall::BlockerHelper::backends::bgp_rtbh;
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::bgp_rtbh - BGP Remote Triggered Black Hole backend (via ExaBGP, GoBGP, or FRR).
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;
25  
26 my $fw_helper = Net::Firewall::BlockerHelper->new(
27 backend => 'bgp_rtbh',
28 name => 'rtbh',
29 options => {
30 next_hop => '192.0.2.1',
31 community => '65535:666',
32 },
33 );
34  
35 $fw_helper->init_backend;
36 $fw_helper->ban( ban => '1.2.3.4' );
37 $fw_helper->unban( ban => '1.2.3.4' );
38 $fw_helper->teardown;
39  
40 =head1 DESCRIPTION
41  
42 Blocks IPs with BGP Remote Triggered Black Holing. Rather than filtering
43 locally, each banned IP is announced to the network as a host route
44 (C<< /32 >> for IPv4, C<< /128 >> for IPv6) carrying the well-known
45 BLACKHOLE community (C<65535:666>, RFC 7999). Upstream routers that honor it
46 then drop the traffic, moving the drop off this host and, with a transit
47 provider that accepts blackhole announcements, off the local link entirely.
48  
49 Announcements are driven through one of three drivers, selected with the
50 C<driver> option: ExaBGP's C<exabgpcli> (the default), C<gobgp>, or C<frr>.
51 The exabgp and gobgp drivers talk to a running daemon that holds the BGP
52 session(s) to the routers, while the frr driver injects a blackhole static
53 route via C<vtysh> for a running bgpd to redistribute. This backend does not
54 manage that session, it only announces and withdraws routes.
55  
56 Whether the announced prefix blackholes the traffic's B<source> or
57 B<destination> depends on the receiving router: destination based RTBH drops
58 traffic toward the prefix, while source based RTBH (loose uRPF plus the
59 blackhole community) drops traffic B<from> it. As this tool bans attacker
60 source addresses, a source based RTBH setup is the usual pairing.
61  
62 Requires the configured driver's binary in the C<PATH> (C<exabgpcli>,
63 C<gobgp>, or C<vtysh>) and a running, configured daemon behind it (C<exabgp>,
64 C<gobgpd>, or FRR's C<bgpd>).
65  
66 =head1 METHODS
67  
68 =head2 new
69  
70 Initiates the object. Errors are fatal with this method.
71  
72 - options :: A hash of options. See below.
73 - name :: Required by Net::Firewall::BlockerHelper, otherwise unused.
74  
75 The options hash accepts the following.
76  
77 - driver :: Which BGP daemon to drive, 'exabgp', 'gobgp', or 'frr'. The
78 frr driver injects a blackhole static route via vtysh (which a
79 redistribute-static route-map on the router tags with the blackhole
80 community for BGP); it does not use next_hop/community directly.
81 - Default :: exabgp
82  
83 - announce_type :: 'rtbh' (announce a blackholed host route) or 'flowspec'
84 (announce a FlowSpec rule discarding traffic from the source IP).
85 flowspec is supported by the exabgp and gobgp drivers only.
86 - Default :: rtbh
87  
88 - exabgpcli_cmd :: Path to the exabgpcli binary (driver 'exabgp').
89 - Default :: exabgpcli
90  
91 - gobgp_cmd :: Path to the gobgp binary (driver 'gobgp').
92 - Default :: gobgp
93  
94 - vtysh_cmd :: Path to the vtysh binary (driver 'frr').
95 - Default :: vtysh
96  
97 - community :: BGP community attached to every announced route. The
98 default is the RFC 7999 well-known BLACKHOLE community.
99 - Default :: 65535:666
100  
101 - next_hop :: Next hop for IPv4 announcements. On the receiving router
102 this is what maps to the discard interface.
103 - Default :: 192.0.2.1
104  
105 - next_hop6 :: Next hop for IPv6 announcements.
106 - Default :: 100::1
107  
108 - mask4 :: Prefix length used for IPv4 announcements.
109 - Default :: 32
110  
111 - mask6 :: Prefix length used for IPv6 announcements.
112 - Default :: 128
113  
114 - extra :: Optional extra attributes appended verbatim to each announce.
115 The syntax is driver specific (exabgp 'local-preference 50' vs
116 gobgp 'local-pref 50').
117 - Default :: undef
118  
119 All errors are considered fatal, meaning if new fails it will die.
120  
121 =cut
122  
123 sub new {
124 9 ▶ my ( $blank, %opts ) = @_;
Subroutine new: called CC=23 SCAR=33.9
Pod documented
125  
126 9 my $self = {
127 perror => undef,
128 error => undef,
129 errorLine => undef,
130 errorFilename => undef,
131 errorString => "",
132 errorExtra => {
133 all_errors_fatal => 1,
134 # all_fatal is what Error::Helper 2.1.0 actually checks; all_errors_fatal
135 # is kept for the name documented in its POD
136 all_fatal => 1,
137 flags => {
138 1 => 'notInited',
139 8 => 'optionsNotHash',
140 9 => 'noBanItem',
141 10 => 'banItemNotIP',
142 12 => 'backendInitError',
143 13 => 'banFailed',
144 14 => 'unbanFailed',
145 15 => 'listFailed',
146 16 => 'reInitFailed',
147 17 => 'teardownFailed',
148 18 => 'alreadyInited',
149 20 => 'driverInvalid',
150 21 => 'announceTypeInvalid',
151 24 => 'checkFailed',
152 25 => 'flushFailed',
153 26 => 'banCidrFailed',
154 27 => 'unbanCidrFailed',
155 28 => 'cidrItemNotCidr',
156 29 => 'cidrNotSupported',
157 30 => 'listCidrFailed',
158 },
159 fatal_flags => {},
160 perror_not_fatal => 0,
161 },
162 options => {},
163 ports => [],
164 protocols => [],
165 testing => undef,
166 test_data => undef,
167 prefix => 'kur',
168 name => undef,
169 frontend_obj => undef,
170 inited => 0,
171 banned => {},
172 banned_cidr => {},
173 cidr_supported => 1,
174 };
175 9 bless $self;
176  
177 9 ▶ if ( defined( $opts{testing} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"testing"}) 9 0 9
178 9 $self->{testing} = $opts{testing};
179 }
180 9 ▶ if ( defined( $opts{frontend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"frontend_obj"}) 9 0 9
181 9 $self->{frontend_obj} = $opts{frontend_obj};
182 }
183 9 ▶ if ( defined( $opts{prefix} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"prefix"}) 9 0 9
184 9 $self->{prefix} = $opts{prefix};
185 }
186 9 ▶ if ( defined( $opts{name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"name"}) 9 0 9
187 9 $self->{name} = $opts{name};
188 }
189  
190 9 ▶ if ( defined( $opts{options} ) && ref( $opts{options} ) ne 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'options'} and ref $opts{'options'} ne 'HASH') { } 0 9 9
elsif (defined $opts{'options'}) { } 9 0 9
Condition33%1/3
defined $opts{"options"} and ref $opts{"options"} ne "HASH"
!ll&&!rl&&r
090
MC/DC0%0/2
defined $opts{"options"} and ref $opts{"options"} ne "HASH"
defined $opts{"options"} ref $opts{"options"} ne "HASH"
Truth table33%1/3
A and B
A= defined $opts{"options"}B= ref $opts{"options"} ne "HASH"
ABresult
0X0
100
111
191 0 $self->{perror} = 1;
192 0 $self->{error} = 8;
193 0 $self->{errorString} = 'ref for options is "' . ref( $opts{options} ) . '" and not HASH';
194 0 $self->warn;
195 } elsif ( defined( $opts{options} ) ) {
196 9 $self->{options} = $opts{options};
197 }
198  
199 # defaults
200 9 ▶ $self->{options}{driver} = 'exabgp' if ( !defined( $self->{options}{driver} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"driver"} 3 6 9
201 9 ▶ $self->{options}{announce_type} = 'rtbh' if ( !defined( $self->{options}{announce_type} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"announce_type"} 6 3 9
202 9 ▶ $self->{options}{exabgpcli_cmd} = 'exabgpcli' if ( !defined( $self->{options}{exabgpcli_cmd} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"exabgpcli_cmd"} 8 1 9
203 9 ▶ $self->{options}{gobgp_cmd} = 'gobgp' if ( !defined( $self->{options}{gobgp_cmd} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"gobgp_cmd"} 9 0 9
204 9 ▶ $self->{options}{vtysh_cmd} = 'vtysh' if ( !defined( $self->{options}{vtysh_cmd} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"vtysh_cmd"} 9 0 9
205 9 ▶ $self->{options}{community} = '65535:666' if ( !defined( $self->{options}{community} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"community"} 8 1 9
206 9 ▶ $self->{options}{next_hop} = '192.0.2.1' if ( !defined( $self->{options}{next_hop} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"next_hop"} 6 3 9
207 9 ▶ $self->{options}{next_hop6} = '100::1' if ( !defined( $self->{options}{next_hop6} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"next_hop6"} 8 1 9
208 9 ▶ $self->{options}{mask4} = 32 if ( !defined( $self->{options}{mask4} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"mask4"} 8 1 9
209 9 ▶ $self->{options}{mask6} = 128 if ( !defined( $self->{options}{mask6} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"mask6"} 9 0 9
210  
211 9 my %valid_drivers = ( exabgp => 1, gobgp => 1, frr => 1 );
212 9 ▶ if ( !$valid_drivers{ $self->{options}{driver} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($valid_drivers{$self->{"options"}{"driver"}}) 1 8 9
213 1 $self->{perror} = 1;
214 1 $self->{error} = 20;
215 1 $self->{errorString} = 'driver is "' . $self->{options}{driver} . '" and not "exabgp", "gobgp", or "frr"';
216 1 $self->warn;
217 }
218  
219 8 ▶ if ( $self->{options}{announce_type} ne 'rtbh' && $self->{options}{announce_type} ne 'flowspec' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'options'}{'announce_type'} ne 'rtbh' and $self->{'options'}{'announce_type'} ne 'flowspec') { } 0 8 8
elsif ($self->{'options'}{'announce_type'} eq 'flowspec' and $self->{'options'}{'driver'} eq 'frr') { } 1 12 13
Condition66%2/3
$self->{"options"}{"announce_type"} ne "rtbh" and $self->{"options"}{"announce_type"} ne "flowspec"
!ll&&!rl&&r
530
MC/DC0%0/2
$self->{"options"}{"announce_type"} ne "rtbh" and $self->{"options"}{"announce_type"} ne "flowspec"
$self->{"options"}{"announce_type"} ne "rtbh" $self->{"options"}{"announce_type"} ne "flowspec"
Truth table66%2/3
A and B
A= $self->{"options"}{"announce_type"} ne "rtbh"B= $self->{"options"}{"announce_type"} ne "flowspec"
ABresult
0X0
100
111
220 0 $self->{perror} = 1;
221 0 $self->{error} = 21;
222 0 $self->{errorString} = 'announce_type is "' . $self->{options}{announce_type} . '" and not "rtbh" or "flowspec"';
223 0 $self->warn;
224 ▶ } elsif ( $self->{options}{announce_type} eq 'flowspec' && $self->{options}{driver} eq 'frr' ) {
Condition100%3/3
$self->{"options"}{"announce_type"} eq "flowspec" and $self->{"options"}{"driver"} eq "frr"
!ll&&!rl&&r
521
MC/DC100%2/2
$self->{"options"}{"announce_type"} eq "flowspec" and $self->{"options"}{"driver"} eq "frr"
$self->{"options"}{"announce_type"} eq "flowspec" $self->{"options"}{"driver"} eq "frr"
Truth table100%3/3
A and B
A= $self->{"options"}{"announce_type"} eq "flowspec"B= $self->{"options"}{"driver"} eq "frr"
ABresult
0X0
100
111
225 # frr's vtysh driver only injects blackhole routes, not flowspec rules
226 1 $self->{perror} = 1;
227 1 $self->{error} = 21;
228 1 $self->{errorString} = 'announce_type flowspec is not supported by the frr driver';
229 1 $self->warn;
230 }
231  
232 7 return $self;
233 } ## end sub new
234  
235 # Internal helper. Builds the announce/withdraw command for the given verb
236 # ('announce' or 'withdraw') and IP, in the syntax of the configured driver
237 # (exabgp via exabgpcli or gobgp), picking the family-appropriate next
238 # hop and prefix length and attaching the blackhole community.
239 sub _route_command {
240 16 ▶ my ( $self, $verb, $ip ) = @_;
Subroutine _route_command: called CC=19 SCAR=29.5
241  
242 16 ▶ my $is_v4 = ( $ip =~ /\A$IPv4_re\z/ ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ip =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv4_re\z/ ? : 12 4 16
243 16 ▶ my $mask = $is_v4 ? $self->{options}{mask4} : $self->{options}{mask6};
Branch: 1 branch
BranchTrueFalseTotal
$is_v4 ? : 12 4 16
244 16 ▶ my $nh = $is_v4 ? $self->{options}{next_hop} : $self->{options}{next_hop6};
Branch: 1 branch
BranchTrueFalseTotal
$is_v4 ? : 12 4 16
245 16 ▶ my $extra = ( defined( $self->{options}{extra} ) && $self->{options}{extra} ne '' ) ? $self->{options}{extra} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'options'}{'extra'} && $self->{'options'}{'extra'} ne '' ? : 1 15 16
Condition66%2/3
defined $self->{"options"}{"extra"} and $self->{"options"}{"extra"} ne ""
!ll&&!rl&&r
1501
MC/DC50%1/2
defined $self->{"options"}{"extra"} and $self->{"options"}{"extra"} ne ""
defined $self->{"options"}{"extra"} $self->{"options"}{"extra"} ne ""
Truth table66%2/3
A and B
A= defined $self->{"options"}{"extra"}B= $self->{"options"}{"extra"} ne ""
ABresult
0X0
100
111
246 16 my $prefix = $ip . '/' . $mask;
247 16 ▶ my $is_flowspec = ( $self->{options}{announce_type} eq 'flowspec' ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'options'}{'announce_type'} eq 'flowspec' ? : 2 14 16
248  
249 16 ▶ if ( $self->{options}{driver} eq 'frr' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"driver"} eq "frr") 3 13 16
250 # frr injects a blackhole static route via vtysh; a redistribute-static
251 # route-map on the router tags it with the blackhole community for BGP
252 3 ▶ my $keyword = $is_v4 ? 'ip route' : 'ipv6 route';
Branch: 1 branch
BranchTrueFalseTotal
$is_v4 ? : 2 1 3
253 3 ▶ my $line = ( $verb eq 'announce' ? '' : 'no ' ) . $keyword . ' ' . $prefix . ' blackhole';
Branch: 1 branch
BranchTrueFalseTotal
$verb eq 'announce' ? : 2 1 3
254 3 return $self->{options}{vtysh_cmd} . " -c 'configure terminal' -c '" . $line . "'";
255 }
256  
257 13 ▶ if ( $self->{options}{driver} eq 'gobgp' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"driver"} eq "gobgp") 4 9 13
258 4 ▶ my $op = ( $verb eq 'announce' ) ? 'add' : 'del';
Branch: 1 branch
BranchTrueFalseTotal
$verb eq 'announce' ? : 3 1 4
259  
260 4 ▶ if ($is_flowspec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_flowspec) 1 3 4
261 # gobgp global rib add|del -a ipv4-flowspec match source <prefix> then discard
262 1 ▶ my $afi = $is_v4 ? 'ipv4-flowspec' : 'ipv6-flowspec';
Branch: 1 branch
BranchTrueFalseTotal
$is_v4 ? : 0 1 1
263 return
264 $self->{options}{gobgp_cmd}
265 1 . ' global rib '
266 . $op
267 . ' -a '
268 . $afi
269 . ' match source '
270 . $prefix
271 . ' then discard';
272 } ## end if ($is_flowspec)
273  
274 # gobgp global rib add|del <prefix> [nexthop ..] [community ..] -a <afi>
275 3 ▶ my $afi = $is_v4 ? 'ipv4' : 'ipv6';
Branch: 1 branch
BranchTrueFalseTotal
$is_v4 ? : 2 1 3
276 3 my $cmd = $self->{options}{gobgp_cmd} . ' global rib ' . $op . ' ' . $prefix;
277  
278 # a withdrawal matches on prefix alone; attributes are only needed to add
279 3 ▶ if ( $op eq 'add' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "add") 2 1 3
280 2 $cmd .= ' nexthop ' . $nh . ' community ' . $self->{options}{community};
281 2 ▶ $cmd .= ' ' . $extra if ( $extra ne '' );
Branch: 1 branch
BranchTrueFalseTotal
if $extra ne "" 0 2 2
282 }
283 3 $cmd .= ' -a ' . $afi;
284  
285 3 return $cmd;
286 } ## end if ( $self->{options}{driver...})
287  
288 # exabgp driver; the command is passed single quoted as the braces and
289 # semicolons of the flow syntax are shell syntax when left bare
290 9 ▶ if ($is_flowspec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_flowspec) 1 8 9
291 # exabgpcli 'announce|withdraw flow route { match { source <prefix>; } then { discard; } }'
292 return
293 1 $self->{options}{exabgpcli_cmd} . " '"
294 . $verb
295 . ' flow route { match { source '
296 . $prefix
297 . "; } then { discard; } }'";
298 } ## end if ($is_flowspec)
299  
300 # exabgp rtbh: exabgpcli 'announce|withdraw route <prefix> next-hop .. community [..]'
301 my $route
302 = $verb
303 . ' route '
304 . $prefix
305 . ' next-hop '
306 . $nh
307 8 . ' community [' . $self->{options}{community} . ']';
308 8 ▶ $route .= ' ' . $extra if ( $extra ne '' );
Branch: 1 branch
BranchTrueFalseTotal
if $extra ne "" 1 7 8
309  
310 # single quoted so the community's [..] can not glob against the cwd
311 8 return $self->{options}{exabgpcli_cmd} . " '" . $route . "'";
312 } ## end sub _route_command
313  
314 # Internal helper. Like _route_command but for a CIDR range. The passed
315 # value is already an address/prefix, so it is announced/withdrawn verbatim
316 # rather than having a host mask appended, with the family determined from the
317 # address portion of the range.
318 sub _route_command_cidr {
319 3 ▶ my ( $self, $verb, $cidr ) = @_;
Subroutine _route_command_cidr: called CC=19 SCAR=46.4
320  
321 3 ▶ my $addr = ( $cidr =~ m!\A(.+)/[0-9]{1,3}\z! ) ? $1 : $cidr;
Branch: 1 branch
BranchTrueFalseTotal
$cidr =~ m[\A(.+)/[0-9]{1,3}\z] ? : 3 0 3
322 3 ▶ my $is_v4 = ( $addr =~ /\A$IPv4_re\z/ ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv4_re\z/ ? : 2 1 3
323 3 ▶ my $nh = $is_v4 ? $self->{options}{next_hop} : $self->{options}{next_hop6};
Branch: 1 branch
BranchTrueFalseTotal
$is_v4 ? : 2 1 3
324 3 ▶ my $extra = ( defined( $self->{options}{extra} ) && $self->{options}{extra} ne '' ) ? $self->{options}{extra} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'options'}{'extra'} && $self->{'options'}{'extra'} ne '' ? : 0 3 3
Condition33%1/3
defined $self->{"options"}{"extra"} and $self->{"options"}{"extra"} ne ""
!ll&&!rl&&r
300
MC/DC0%0/2
defined $self->{"options"}{"extra"} and $self->{"options"}{"extra"} ne ""
defined $self->{"options"}{"extra"} $self->{"options"}{"extra"} ne ""
Truth table33%1/3
A and B
A= defined $self->{"options"}{"extra"}B= $self->{"options"}{"extra"} ne ""
ABresult
0X0
100
111
325 3 my $prefix = $cidr;
326 3 ▶ my $is_flowspec = ( $self->{options}{announce_type} eq 'flowspec' ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'options'}{'announce_type'} eq 'flowspec' ? : 0 3 3
327  
328 3 ▶ if ( $self->{options}{driver} eq 'frr' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"driver"} eq "frr") 0 3 3
329 # frr injects a blackhole static route via vtysh; a redistribute-static
330 # route-map on the router tags it with the blackhole community for BGP
331 0 ▶ my $keyword = $is_v4 ? 'ip route' : 'ipv6 route';
Branch: 1 branch
BranchTrueFalseTotal
$is_v4 ? : 0 0 0
332 0 ▶ my $line = ( $verb eq 'announce' ? '' : 'no ' ) . $keyword . ' ' . $prefix . ' blackhole';
Branch: 1 branch
BranchTrueFalseTotal
$verb eq 'announce' ? : 0 0 0
333 0 return $self->{options}{vtysh_cmd} . " -c 'configure terminal' -c '" . $line . "'";
334 }
335  
336 3 ▶ if ( $self->{options}{driver} eq 'gobgp' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"driver"} eq "gobgp") 0 3 3
337 0 ▶ my $op = ( $verb eq 'announce' ) ? 'add' : 'del';
Branch: 1 branch
BranchTrueFalseTotal
$verb eq 'announce' ? : 0 0 0
338  
339 0 ▶ if ($is_flowspec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_flowspec) 0 0 0
340 # gobgp global rib add|del -a ipv4-flowspec match source <prefix> then discard
341 0 ▶ my $afi = $is_v4 ? 'ipv4-flowspec' : 'ipv6-flowspec';
Branch: 1 branch
BranchTrueFalseTotal
$is_v4 ? : 0 0 0
342 return
343 $self->{options}{gobgp_cmd}
344 0 . ' global rib '
345 . $op
346 . ' -a '
347 . $afi
348 . ' match source '
349 . $prefix
350 . ' then discard';
351 } ## end if ($is_flowspec)
352  
353 # gobgp global rib add|del <prefix> [nexthop ..] [community ..] -a <afi>
354 0 ▶ my $afi = $is_v4 ? 'ipv4' : 'ipv6';
Branch: 1 branch
BranchTrueFalseTotal
$is_v4 ? : 0 0 0
355 0 my $cmd = $self->{options}{gobgp_cmd} . ' global rib ' . $op . ' ' . $prefix;
356  
357 # a withdrawal matches on prefix alone; attributes are only needed to add
358 0 ▶ if ( $op eq 'add' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "add") 0 0 0
359 0 $cmd .= ' nexthop ' . $nh . ' community ' . $self->{options}{community};
360 0 ▶ $cmd .= ' ' . $extra if ( $extra ne '' );
Branch: 1 branch
BranchTrueFalseTotal
if $extra ne "" 0 0 0
361 }
362 0 $cmd .= ' -a ' . $afi;
363  
364 0 return $cmd;
365 } ## end if ( $self->{options}{driver...})
366  
367 # exabgp driver; the command is passed single quoted as the braces and
368 # semicolons of the flow syntax are shell syntax when left bare
369 3 ▶ if ($is_flowspec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_flowspec) 0 3 3
370 # exabgpcli 'announce|withdraw flow route { match { source <prefix>; } then { discard; } }'
371 return
372 0 $self->{options}{exabgpcli_cmd} . " '"
373 . $verb
374 . ' flow route { match { source '
375 . $prefix
376 . "; } then { discard; } }'";
377 } ## end if ($is_flowspec)
378  
379 # exabgp rtbh: exabgpcli 'announce|withdraw route <prefix> next-hop .. community [..]'
380 my $route
381 = $verb
382 . ' route '
383 . $prefix
384 . ' next-hop '
385 . $nh
386 3 . ' community [' . $self->{options}{community} . ']';
387 3 ▶ $route .= ' ' . $extra if ( $extra ne '' );
Branch: 1 branch
BranchTrueFalseTotal
if $extra ne "" 0 3 3
388  
389 # single quoted so the community's [..] can not glob against the cwd
390 3 return $self->{options}{exabgpcli_cmd} . " '" . $route . "'";
391 } ## end sub _route_command_cidr
392  
393 # Internal helper. Returns the driver-appropriate command used by check to
394 # confirm the BGP daemon is reachable.
395 sub _check_command {
396 19 ▶ my ($self) = @_;
Subroutine _check_command: called CC=3 SCAR=11.0
397  
398 19 ▶ if ( $self->{options}{driver} eq 'frr' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"driver"} eq "frr") 3 16 19
399 3 return $self->{options}{vtysh_cmd} . " -c 'show ip bgp summary'";
400 }
401 16 ▶ if ( $self->{options}{driver} eq 'gobgp' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"driver"} eq "gobgp") 5 11 16
402 5 return $self->{options}{gobgp_cmd} . ' neighbor';
403 }
404  
405 11 return $self->{options}{exabgpcli_cmd} . ' show neighbor summary';
406 } ## end sub _check_command
407  
408 =head2 init
409  
410 Initiates the backend. The BGP session is owned by the running BGP daemon
411 (exabgp, gobgpd, or FRR), so there is nothing to set up; this only flips the
412 inited flag.
413  
414 =cut
415  
416 sub init {
417 7 ▶ my ( $self, %opts ) = @_;
Subroutine init: called CC=3 SCAR=12.6
Pod documented
418  
419 7 $self->errorblank;
420  
421 7 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 7 7
422 0 $self->{error} = 18;
423 0 $self->{errorString} = 'backend has already been inited';
424 0 $self->warn;
425 }
426  
427 7 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 7 0 7
428 7 $self->{frontend_obj}->{test_data} = [];
429 }
430  
431 7 $self->{inited} = 1;
432 } ## end sub init
433  
434 =head2 ban
435  
436 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address
437 and lowercased, then announced via the configured driver: a
438 C<exabgpcli announce route> or C<gobgp global rib add> of the host route
439 (C<mask4>/C<mask6> prefix length) with the family-appropriate next hop and
440 the blackhole community, a FlowSpec discard rule when announce_type is
441 C<flowspec>, or a C<vtysh> injected blackhole static route for the frr
442 driver. Banning an already banned IP is a noop.
443  
444 $fw_helper->ban( ban => $ip );
445  
446 =cut
447  
448 sub ban {
449 12 ▶ my ( $self, %opts ) = @_;
Subroutine ban: called CC=10 SCAR=33.4
Pod documented
450  
451 12 $self->errorblank;
452  
453 12 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 12 12
454 0 $self->{error} = 1;
455 0 $self->{errorString} = 'backend has not been inited';
456 0 $self->warn;
457 0 return;
458 }
459  
460 12 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 12 12
elsif (ref $opts{'ban'} ne '') { } 0 12 12
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv6_re\z/) { } 0 12 12
461 0 $self->{error} = 9;
462 0 $self->{errorString} = 'Nothing specified for the value ban';
463 0 $self->warn;
464 0 return;
465 } elsif ( ref( $opts{ban} ) ne '' ) {
466 0 $self->{error} = 10;
467 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
468 0 $self->warn;
469 0 return;
470 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition66%2/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv6_re\z/
l!l&&r!l&&!r
750
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv6_re\z/
Truth table66%2/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv6_re\z/
ABresult
1X1
011
000
471 && $opts{ban} !~ /\A$IPv6_re\z/ )
472 {
473 0 $self->{error} = 10;
474 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
475 0 $self->warn;
476 0 return;
477 }
478  
479 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
480 12 $opts{ban} = lc( $opts{ban} );
481  
482 12 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 1 11 12
483 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 1 0 1
484 1 $self->{frontend_obj}->{test_data} = 'already banned';
485 }
486 1 return;
487 }
488  
489 11 my $command = $self->_route_command( 'announce', $opts{ban} );
490  
491 11 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 11 0 11
492 11 $self->{frontend_obj}->{test_data} = $command;
493 } else {
494 0 my $output = `$command 2>&1`;
495 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
496 0 $self->{error} = 13;
497 $self->{errorString}
498 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
499 0 $self->warn;
500 }
501 }
502  
503 11 $self->{banned}{ $opts{ban} } = 1;
504 } ## end sub ban
505  
506 =head2 unban
507  
508 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
509 and lowercased, then the route announced for it by L</ban> is withdrawn via
510 the configured driver (C<exabgpcli withdraw>, C<gobgp global rib del>, or a
511 C<vtysh> C<no ... blackhole> for frr). Unbanning an IP that is not banned is
512 a noop.
513  
514 $fw_helper->unban( ban => $ip );
515  
516 =cut
517  
518 sub unban {
519 3 ▶ my ( $self, %opts ) = @_;
Subroutine unban: called CC=10 SCAR=37.2
Pod documented
520  
521 3 $self->errorblank;
522  
523 3 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 3 3
524 0 $self->{error} = 1;
525 0 $self->{errorString} = 'backend has not been inited';
526 0 $self->warn;
527 0 return;
528 }
529  
530 3 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 3 3
elsif (ref $opts{'ban'} ne '') { } 0 3 3
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv6_re\z/) { } 0 3 3
531 0 $self->{error} = 9;
532 0 $self->{errorString} = 'Nothing specified for the value ban';
533 0 $self->warn;
534 0 return;
535 } elsif ( ref( $opts{ban} ) ne '' ) {
536 0 $self->{error} = 10;
537 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
538 0 $self->warn;
539 0 return;
540 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition33%1/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv6_re\z/
l!l&&r!l&&!r
300
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv6_re\z/
Truth table33%1/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv6_re\z/
ABresult
1X1
011
000
541 && $opts{ban} !~ /\A$IPv6_re\z/ )
542 {
543 0 $self->{error} = 10;
544 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
545 0 $self->warn;
546 0 return;
547 }
548  
549 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
550 3 $opts{ban} = lc( $opts{ban} );
551  
552 3 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 3 3
553 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
554 0 $self->{frontend_obj}->{test_data} = 'not banned';
555 }
556 0 return;
557 }
558  
559 3 my $command = $self->_route_command( 'withdraw', $opts{ban} );
560  
561 3 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 3 0 3
562 3 $self->{frontend_obj}->{test_data} = $command;
563 } else {
564 0 my $output = `$command 2>&1`;
565 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
566 0 $self->{error} = 14;
567 $self->{errorString}
568 0 = 'unban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
569 0 $self->warn;
570 }
571 }
572  
573 3 delete( $self->{banned}{ $opts{ban} } );
574 } ## end sub unban
575  
576 # Internal helper. Returns a true value if the passed scalar is a valid IPv4 or
577 # IPv6 CIDR range, that is an address followed by "/" and a prefix length that
578 # is within the range valid for its family (0 to 32 for IPv4, 0 to 128 for
579 # IPv6). Returns false otherwise.
580 sub _valid_cidr {
581 3 ▶ my ( $self, $cidr ) = @_;
Subroutine _valid_cidr: called CC=8 SCAR=24.3
582  
583 3 ▶ return 0 if ( !defined($cidr) || ref($cidr) ne '' );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $cidr or ref $cidr ne "" 0 3 3
Condition33%1/3
not defined $cidr or ref $cidr ne ""
l!l&&r!l&&!r
003
MC/DC0%0/2
not defined $cidr or ref $cidr ne ""
not defined $cidr ref $cidr ne ""
Truth table33%1/3
not(A) or B
A= not defined $cidrB= ref $cidr ne ""
ABresult
1X1
011
000
584  
585 3 ▶ if ( $cidr =~ m!\A(.+)/([0-9]{1,3})\z! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cidr =~ m[\A(.+)/([0-9]{1,3})\z]) 3 0 3
586 3 my ( $addr, $prefix ) = ( $1, $2 );
587 3 ▶ return 1 if ( $addr =~ /\A$IPv4_re\z/ && $prefix <= 32 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv4_re\z/ and $prefix <= 32 2 1 3
Condition66%2/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv4_re\z/ and $prefix <= 32
!ll&&!rl&&r
102
MC/DC50%1/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv4_re\z/ and $prefix <= 32
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv4_re\z/ $prefix <= 32
Truth table66%2/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv4_re\z/B= $prefix <= 32
ABresult
0X0
100
111
588 1 ▶ return 1 if ( $addr =~ /\A$IPv6_re\z/ && $prefix <= 128 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv6_re\z/ and $prefix <= 128 1 0 1
Condition33%1/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv6_re\z/ and $prefix <= 128
!ll&&!rl&&r
001
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv6_re\z/ and $prefix <= 128
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv6_re\z/ $prefix <= 128
Truth table33%1/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::bgp_rtbh::IPv6_re\z/B= $prefix <= 128
ABresult
0X0
100
111
589 }
590  
591 0 return 0;
592 } ## end sub _valid_cidr
593  
594 =head2 ban_cidr
595  
596 Bans a CIDR range. The value of ban is validated as being a IPv4 or IPv6
597 CIDR range and lowercased, then announced the same way L</ban> announces a
598 single IP, except the range is announced verbatim rather than having the
599 C<mask4>/C<mask6> host prefix length appended. Banning an already banned
600 range is a noop.
601  
602 $fw_helper->ban_cidr( ban => '1.2.3.0/24' );
603  
604 =cut
605  
606 sub ban_cidr {
607 2 ▶ my ( $self, %opts ) = @_;
Subroutine ban_cidr: called CC=9 SCAR=35.4
Pod documented
608  
609 2 $self->errorblank;
610  
611 2 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 2 2
612 0 $self->{error} = 1;
613 0 $self->{errorString} = 'backend has not been inited';
614 0 $self->warn;
615 0 return;
616 }
617  
618 2 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 2 2
elsif (ref $opts{'ban'} ne '') { } 0 2 2
elsif (not $self->_valid_cidr($opts{'ban'})) { } 0 2 2
619 0 $self->{error} = 9;
620 0 $self->{errorString} = 'Nothing specified for the value ban';
621 0 $self->warn;
622 0 return;
623 } elsif ( ref( $opts{ban} ) ne '' ) {
624 0 $self->{error} = 28;
625 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
626 0 $self->warn;
627 0 return;
628 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
629 0 $self->{error} = 28;
630 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
631 0 $self->warn;
632 0 return;
633 }
634  
635 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
636 2 $opts{ban} = lc( $opts{ban} );
637  
638 2 ▶ if ( $self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned_cidr"}{$opts{"ban"}}) 0 2 2
639 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
640 0 $self->{frontend_obj}->{test_data} = 'already banned';
641 }
642 0 return;
643 }
644  
645 2 my $command = $self->_route_command_cidr( 'announce', $opts{ban} );
646  
647 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
648 2 $self->{frontend_obj}->{test_data} = $command;
649 } else {
650 0 my $output = `$command 2>&1`;
651 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
652 0 $self->{error} = 26;
653 $self->{errorString}
654 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
655 0 $self->warn;
656 }
657 }
658  
659 2 $self->{banned_cidr}{ $opts{ban} } = 1;
660 } ## end sub ban_cidr
661  
662 =head2 unban_cidr
663  
664 Unbans a CIDR range. The value of ban is validated as being a IPv4 or IPv6
665 CIDR range and lowercased, then the route announced for it by L</ban_cidr>
666 is withdrawn via the configured driver. Unbanning a range that is not banned
667 is a noop.
668  
669 $fw_helper->unban_cidr( ban => '1.2.3.0/24' );
670  
671 =cut
672  
673 sub unban_cidr {
674 1 ▶ my ( $self, %opts ) = @_;
Subroutine unban_cidr: called CC=9 SCAR=35.4
Pod documented
675  
676 1 $self->errorblank;
677  
678 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
679 0 $self->{error} = 1;
680 0 $self->{errorString} = 'backend has not been inited';
681 0 $self->warn;
682 0 return;
683 }
684  
685 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 $self->_valid_cidr($opts{'ban'})) { } 0 1 1
686 0 $self->{error} = 9;
687 0 $self->{errorString} = 'Nothing specified for the value ban';
688 0 $self->warn;
689 0 return;
690 } elsif ( ref( $opts{ban} ) ne '' ) {
691 0 $self->{error} = 28;
692 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
693 0 $self->warn;
694 0 return;
695 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
696 0 $self->{error} = 28;
697 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
698 0 $self->warn;
699 0 return;
700 }
701  
702 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
703 1 $opts{ban} = lc( $opts{ban} );
704  
705 1 ▶ if ( !$self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
706 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
707 0 $self->{frontend_obj}->{test_data} = 'not banned';
708 }
709 0 return;
710 }
711  
712 1 my $command = $self->_route_command_cidr( 'withdraw', $opts{ban} );
713  
714 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
715 1 $self->{frontend_obj}->{test_data} = $command;
716 } else {
717 0 my $output = `$command 2>&1`;
718 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
719 0 $self->{error} = 27;
720 $self->{errorString}
721 0 = 'unban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
722 0 $self->warn;
723 }
724 }
725  
726 1 delete( $self->{banned_cidr}{ $opts{ban} } );
727 } ## end sub unban_cidr
728  
729 =head2 list_cidr
730  
731 List banned CIDR ranges. Returns an array of the currently banned ranges
732 from internal state; the BGP daemon is not queried.
733  
734 my @banned_cidrs = $fw_helper->list_cidr;
735  
736 =cut
737  
738 sub list_cidr {
739 2 ▶ my ( $self, %opts ) = @_;
Subroutine list_cidr: called CC=2 SCAR=7.0
Pod documented
740  
741 2 $self->errorblank;
742  
743 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
744 2 $self->{frontend_obj}->{test_data} = 'list_cidr';
745 }
746  
747 2 return keys( %{ $self->{banned_cidr} } );
748 }
749  
750 =head2 list
751  
752 List banned IPs. Returns an array of the currently banned single IPs from
753 internal state; the BGP daemon is not queried. CIDR ranges are not included;
754 for those see L</list_cidr>.
755  
756 my @banned = $fw_helper->list;
757  
758 =cut
759  
760 sub list {
761 1 ▶ my ( $self, %opts ) = @_;
Subroutine list: called CC=2 SCAR=7.0
Pod documented
762  
763 1 $self->errorblank;
764  
765 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 1 0 1
766 1 $self->{frontend_obj}->{test_data} = 'list';
767 }
768  
769 1 return keys( %{ $self->{banned} } );
770 }
771  
772 =head2 re_init
773  
774 Re-announces every retained blackhole route. A best effort teardown
775 (failures ignored) is done first, then the announce command is re-run for
776 every banned IP and CIDR range. The BGP daemons do not persist announced
777 state across restarts, so this is how the announcements are restored after
778 the daemon is bounced.
779  
780 =cut
781  
782 sub re_init {
783 0 ▶ my ( $self, %opts ) = @_;
Subroutine re_init: not called CC=9 SCAR=45.0
Pod documented
784  
785 0 $self->errorblank;
786  
787 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
788 0 $self->{error} = 1;
789 0 $self->{errorString} = 'backend has not been inited';
790 0 $self->warn;
791 0 return;
792 }
793  
794 # teardown is best effort; a session that already dropped the routes is
795 # exactly what re_init is meant to recover from
796 {
797 0 local $@;
798 0 eval { $self->teardown; };
799 }
800 0 $self->init;
801  
802 0 my @re_init_test_data;
803 0 foreach my $item ( keys( %{ $self->{banned} } ) ) {
804 0 my $command = $self->_route_command( 'announce', $item );
805 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
806 0 push( @re_init_test_data, $command );
807 } else {
808 0 my $output = `$command 2>&1`;
809 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
810 0 $self->{error} = 13;
811 $self->{errorString}
812 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
813 0 $self->warn;
814 }
815 }
816 } ## end foreach my $item ( keys( %{ ...}))
817  
818 0 foreach my $item ( keys( %{ $self->{banned_cidr} } ) ) {
819 0 my $command = $self->_route_command_cidr( 'announce', $item );
820 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
821 0 push( @re_init_test_data, $command );
822 } else {
823 0 my $output = `$command 2>&1`;
824 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
825 0 $self->{error} = 26;
826 $self->{errorString}
827 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
828 0 $self->warn;
829 }
830 }
831 } ## end foreach my $item ( keys( %{ ...}))
832  
833 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
834 0 $self->{frontend_obj}->{test_data} = \@re_init_test_data;
835 }
836  
837 0 $self->{inited} = 1;
838 } ## end sub re_init
839  
840 =head2 teardown
841  
842 Withdraws every announced blackhole route, running the driver's withdraw
843 command for each banned IP and CIDR range, and marks the backend as not
844 inited. The ban list is retained, so L</re_init> can restore the
845 announcements.
846  
847 =cut
848  
849 sub teardown {
850 1 ▶ my ( $self, %opts ) = @_;
Subroutine teardown: called CC=6 SCAR=22.4
Pod documented
851  
852 1 $self->errorblank;
853  
854 1 $self->{inited} = 0;
855  
856 1 my @commands;
857 1 foreach my $item ( keys( %{ $self->{banned} } ) ) {
858 2 push( @commands, $self->_route_command( 'withdraw', $item ) );
859 }
860 1 foreach my $item ( keys( %{ $self->{banned_cidr} } ) ) {
861 0 push( @commands, $self->_route_command_cidr( 'withdraw', $item ) );
862 }
863  
864 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
865 1 $self->{frontend_obj}->{test_data} = \@commands;
866 } else {
867 0 foreach my $item (@commands) {
868 0 my $output = `$item 2>&1`;
869 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
870 0 $self->{error} = 17;
871 $self->{errorString}
872 0 = 'teardown failed. non-zero exit code for the command... "' . $item . '"... output... ' . $output;
873 0 $self->warn;
874 }
875 }
876 } ## end else [ if ( $self->{testing} ) ]
877 } ## end sub teardown
878  
879 =head2 stop
880  
881 Alias for L</teardown>.
882  
883 =cut
884  
885 sub stop {
886 0 ▶ my ( $self, %opts ) = @_;
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
887  
888 0 return $self->teardown(%opts);
889 }
890  
891 =head2 check
892  
893 Runs the driver's neighbor summary command (C<exabgpcli show neighbor
894 summary>, C<gobgp neighbor>, or C<vtysh -c 'show ip bgp summary'> for frr)
895 and treats a zero exit as healthy, returning 1, and 0 otherwise. This only
896 confirms the BGP daemon holding the announcements is reachable; whether the
897 sessions to the routers are up and the announcements accepted is not
898 checked.
899  
900 =cut
901  
902 sub check {
903 19 ▶ my ( $self, %opts ) = @_;
Subroutine check: called CC=3 SCAR=13.0
Pod documented
904  
905 19 $self->errorblank;
906  
907 19 my $command = $self->_check_command;
908  
909 19 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 19 0 19
910 19 $self->{frontend_obj}->{test_data} = $command;
911 19 return 1;
912 }
913  
914 0 my $output = `$command 2>&1`;
915 0 ▶ return $? == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$? == 0 ? : 0 0 0
916 } ## end sub check
917  
918 =head2 flush
919  
920 Removes all bans at once by running the driver's withdraw command for every
921 banned IP and CIDR range, then clearing the ban lists.
922  
923 =cut
924  
925 sub flush {
926 0 ▶ my ( $self, %opts ) = @_;
Subroutine flush: not called CC=7 SCAR=40.3
Pod documented
927  
928 0 $self->errorblank;
929  
930 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
931 0 $self->{error} = 1;
932 0 $self->{errorString} = 'backend has not been inited';
933 0 $self->warn;
934 0 return;
935 }
936  
937 0 my @commands;
938 0 foreach my $item ( keys( %{ $self->{banned} } ) ) {
939 0 push( @commands, $self->_route_command( 'withdraw', $item ) );
940 }
941 0 foreach my $item ( keys( %{ $self->{banned_cidr} } ) ) {
942 0 push( @commands, $self->_route_command_cidr( 'withdraw', $item ) );
943 }
944  
945 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
946 0 $self->{frontend_obj}->{test_data} = \@commands;
947 } else {
948 0 foreach my $item (@commands) {
949 0 my $output = `$item 2>&1`;
950 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
951 0 $self->{error} = 25;
952 $self->{errorString}
953 0 = 'flush failed. non-zero exit code for the command... "' . $item . '"... output... ' . $output;
954 0 $self->warn;
955 }
956 }
957 } ## end else [ if ( $self->{testing} ) ]
958  
959 0 $self->{banned} = {};
960 0 $self->{banned_cidr} = {};
961 } ## end sub flush
962  
963 =head1 ERROR CODES / FLAGS
964  
965 Error handling is provided by L<Error::Helper>. All
966 errors are considered fatal.
967  
968 =head2 1, notInited
969  
970 The backend has not been inited yet.
971  
972 =head2 8, optionsNotHash
973  
974 The item passed to new for options is not a hash.
975  
976 =head2 9, noBanItem
977  
978 No IP or CIDR range specified to ban or unban.
979  
980 =head2 10, banItemNotIP
981  
982 The item to ban is not an IP. Either wrong ref type or regexp
983 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
984  
985 =head2 12, backendInitError
986  
987 Failed to init the backend.
988  
989 =head2 13, banFailed
990  
991 Failed to ban the item.
992  
993 =head2 14, unbanFailed
994  
995 Failed to unban the item.
996  
997 =head2 15, listFailed
998  
999 Failed to get a list of bans.
1000  
1001 =head2 16, reInitFailed
1002  
1003 Failed to re_init the backend.
1004  
1005 =head2 17, teardownFailed
1006  
1007 Failed to teardown the backend.
1008  
1009 =head2 18, alreadyInited
1010  
1011 init called, but the backend has already been inited.
1012  
1013 =head2 20, driverInvalid
1014  
1015 The driver option is not one of 'exabgp', 'gobgp', or 'frr'.
1016  
1017 =head2 21, announceTypeInvalid
1018  
1019 The announce_type is not 'rtbh' or 'flowspec', or flowspec was requested with
1020 the frr driver, which does not support it.
1021  
1022 =head2 24, checkFailed
1023  
1024 The backend check raised an error.
1025  
1026 =head2 25, flushFailed
1027  
1028 Failed to flush the bans.
1029  
1030 =head2 26, banCidrFailed
1031  
1032 Failed to ban the CIDR range.
1033  
1034 =head2 27, unbanCidrFailed
1035  
1036 Failed to unban the CIDR range.
1037  
1038 =head2 28, cidrItemNotCidr
1039  
1040 The item to ban is not a CIDR range. Either wrong ref type or it is not an
1041 IPv4 or IPv6 address followed by a prefix length valid for its family.
1042  
1043 =head2 29, cidrNotSupported
1044  
1045 The backend does not support CIDR bans.
1046  
1047 =head2 30, listCidrFailed
1048  
1049 Failed to get a list of CIDR bans.
1050  
1051 =head1 AUTHOR
1052  
1053 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
1054  
1055 =head1 LICENSE AND COPYRIGHT
1056  
1057 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
1058  
1059 This is free software, licensed under:
1060  
1061 The GNU Lesser General Public License, Version 2.1, February 1999
1062  
1063 =cut
1064  
1065 1 1; # End of Net::Firewall::BlockerHelper::backends::bgp_rtbh
« BlockerHelper/backends/abuseipdb.pm ↑ Summary BlockerHelper/backends/routeros.pm »