BlockerHelper/backends/bgp_rtbh.pm

filter: Statementstmt 49.1% 179 / 364 Branchbran 51.9% 107 / 206 Conditioncond 57.1% 12 / 21 MC/DCmcdc 21.4% 3 / 14 Subroutinesub 86.3% 19 / 22 Podpod 100% 13 / 13 totaltotal 52.0% 333 / 640 CC 119 SCAR 36.5
CC
119
Cov
50%
_route_command_cidr
103.1
re_init
72.0
flush
56.0
CRAP 38.5
« BlockerHelper/backends/abuseipdb.pm ↑ Summary BlockerHelper/backends/nsupdate.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 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::bgp_rtbh - BGP Remote Triggered Black Hole backend (via ExaBGP, GoBGP, or FRR).
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 => '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 9 ▶sub new {
Subroutine new: called CC=23 SCAR=33.9
Pod documented
124 9 my ( $blank, %opts ) = @_;
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 command that announces or withdraws the
236 # blackhole route for a single address, in whichever of the three drivers'
237 # syntaxes is configured. This and its CIDR counterpart are where every ban
238 # and unban this backend performs is expressed.
239 #
240 # There is no shared command shape across the drivers, which is why this is
241 # one long branch rather than a template. Each does something structurally
242 # different:
243 #
244 # - frr does not speak BGP here at all. It installs a local blackhole static
245 # route through vtysh, and it is a redistribute-static route-map on the
246 # router that turns that into a tagged BGP announcement. Withdrawal is the
247 # same line prefixed with "no". Because the community is applied by the
248 # router's own configuration, neither the community nor the next hop
249 # option is used on this path.
250 # - gobgp manipulates the RIB directly. A withdrawal matches on the prefix
251 # alone, so the next hop and community are only attached when adding;
252 # sending them on a delete would not match.
253 # - exabgp goes through exabgpcli and takes the whole announcement as one
254 # quoted argument.
255 #
256 # The address is turned into a prefix by appending the configured host mask
257 # for its family, 32 and 128 by default, since what is being blackholed is one
258 # host.
259 #
260 # The exabgp commands are single quoted for two separate reasons worth not
261 # undoing: the flowspec syntax is full of braces and semicolons that the shell
262 # would otherwise treat as its own, and the community is written in square
263 # brackets, which the shell would try to glob against the working directory.
264 #
265 # Args:
266 #
267 # verb - What to do, as a plain string: 'announce' to install the
268 # blackhole or 'withdraw' to remove it. Each driver maps it onto
269 # its own spelling, so gobgp sees add and del and frr sees a line
270 # with or without a leading "no". Anything else is not checked for
271 # and would produce an invalid command.
272 #
273 # ip - The address to blackhole, as a plain string. Expected to be an
274 # already validated and lowercased IPv4 or IPv6 address. The family
275 # is decided by matching against $IPv4_re and selects the mask, the
276 # next hop, and the address family argument.
277 #
278 # Returns the command as a single string ready to hand to the runner. What it
279 # looks like depends entirely on the driver and on whether announce_type is
280 # the default 'rtbh' or 'flowspec'.
281 #
282 # # driver exabgp, announce_type rtbh, community 65535:666
283 # $self->_route_command( 'announce', '10.0.0.1' );
284 # # exabgpcli 'announce route 10.0.0.1/32 next-hop 192.0.2.1 community [65535:666]'
285 #
286 # # the withdrawal is symmetric
287 # $self->_route_command( 'withdraw', '10.0.0.1' );
288 # # exabgpcli 'withdraw route 10.0.0.1/32 next-hop 192.0.2.1 community [65535:666]'
289 #
290 # # driver exabgp, announce_type flowspec
291 # # exabgpcli 'announce flow route { match { source 10.0.0.1/32; } then { discard; } }'
292 #
293 # # driver gobgp: attributes on add
294 # # gobgp global rib add 10.0.0.1/32 nexthop 192.0.2.1 community 65535:666 -a ipv4
295 # # and prefix only on del
296 # # gobgp global rib del 10.0.0.1/32 -a ipv4
297 #
298 # # driver frr, which installs a static route instead
299 # # vtysh -c 'configure terminal' -c 'ip route 10.0.0.1/32 blackhole'
300 # # vtysh -c 'configure terminal' -c 'no ip route 10.0.0.1/32 blackhole'
301 #
302 # The extra option, when set, is appended to the exabgp and gobgp add forms so
303 # further BGP attributes can be carried. It is not applied to withdrawals,
304 # which match on the prefix, nor to the frr path, which has no BGP attributes
305 # of its own.
306 16 ▶sub _route_command {
Subroutine _route_command: called CC=19 SCAR=29.5
307 16 my ( $self, $verb, $ip ) = @_;
308  
309 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
310 16 ▶ my $mask = $is_v4 ? $self->{options}{mask4} : $self->{options}{mask6};
Branch: 1 branch
BranchTrueFalseTotal
$is_v4 ? : 12 4 16
311 16 ▶ my $nh = $is_v4 ? $self->{options}{next_hop} : $self->{options}{next_hop6};
Branch: 1 branch
BranchTrueFalseTotal
$is_v4 ? : 12 4 16
312 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
313 16 my $prefix = $ip . '/' . $mask;
314 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
315  
316 16 ▶ if ( $self->{options}{driver} eq 'frr' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"driver"} eq "frr") 3 13 16
317 # frr injects a blackhole static route via vtysh; a redistribute-static
318 # route-map on the router tags it with the blackhole community for BGP
319 3 ▶ my $keyword = $is_v4 ? 'ip route' : 'ipv6 route';
Branch: 1 branch
BranchTrueFalseTotal
$is_v4 ? : 2 1 3
320 3 ▶ my $line = ( $verb eq 'announce' ? '' : 'no ' ) . $keyword . ' ' . $prefix . ' blackhole';
Branch: 1 branch
BranchTrueFalseTotal
$verb eq 'announce' ? : 2 1 3
321 3 return $self->{options}{vtysh_cmd} . " -c 'configure terminal' -c '" . $line . "'";
322 }
323  
324 13 ▶ if ( $self->{options}{driver} eq 'gobgp' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"driver"} eq "gobgp") 4 9 13
325 4 ▶ my $op = ( $verb eq 'announce' ) ? 'add' : 'del';
Branch: 1 branch
BranchTrueFalseTotal
$verb eq 'announce' ? : 3 1 4
326  
327 4 ▶ if ($is_flowspec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_flowspec) 1 3 4
328 # gobgp global rib add|del -a ipv4-flowspec match source <prefix> then discard
329 1 ▶ my $afi = $is_v4 ? 'ipv4-flowspec' : 'ipv6-flowspec';
Branch: 1 branch
BranchTrueFalseTotal
$is_v4 ? : 0 1 1
330 return
331 $self->{options}{gobgp_cmd}
332 1 . ' global rib '
333 . $op
334 . ' -a '
335 . $afi
336 . ' match source '
337 . $prefix
338 . ' then discard';
339 } ## end if ($is_flowspec)
340  
341 # gobgp global rib add|del <prefix> [nexthop ..] [community ..] -a <afi>
342 3 ▶ my $afi = $is_v4 ? 'ipv4' : 'ipv6';
Branch: 1 branch
BranchTrueFalseTotal
$is_v4 ? : 2 1 3
343 3 my $cmd = $self->{options}{gobgp_cmd} . ' global rib ' . $op . ' ' . $prefix;
344  
345 # a withdrawal matches on prefix alone; attributes are only needed to add
346 3 ▶ if ( $op eq 'add' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "add") 2 1 3
347 2 $cmd .= ' nexthop ' . $nh . ' community ' . $self->{options}{community};
348 2 ▶ $cmd .= ' ' . $extra if ( $extra ne '' );
Branch: 1 branch
BranchTrueFalseTotal
if $extra ne "" 0 2 2
349 }
350 3 $cmd .= ' -a ' . $afi;
351  
352 3 return $cmd;
353 } ## end if ( $self->{options}{driver...})
354  
355 # exabgp driver; the command is passed single quoted as the braces and
356 # semicolons of the flow syntax are shell syntax when left bare
357 9 ▶ if ($is_flowspec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_flowspec) 1 8 9
358 # exabgpcli 'announce|withdraw flow route { match { source <prefix>; } then { discard; } }'
359 return
360 1 $self->{options}{exabgpcli_cmd} . " '"
361 . $verb
362 . ' flow route { match { source '
363 . $prefix
364 . "; } then { discard; } }'";
365 } ## end if ($is_flowspec)
366  
367 # exabgp rtbh: exabgpcli 'announce|withdraw route <prefix> next-hop .. community [..]'
368 my $route
369 = $verb
370 . ' route '
371 . $prefix
372 . ' next-hop '
373 . $nh
374 8 . ' community [' . $self->{options}{community} . ']';
375 8 ▶ $route .= ' ' . $extra if ( $extra ne '' );
Branch: 1 branch
BranchTrueFalseTotal
if $extra ne "" 1 7 8
376  
377 # single quoted so the community's [..] can not glob against the cwd
378 8 return $self->{options}{exabgpcli_cmd} . " '" . $route . "'";
379 } ## end sub _route_command
380  
381 # Internal helper. The CIDR counterpart of _route_command: builds the command
382 # that announces or withdraws the blackhole route for a whole range.
383 #
384 # Blackholing a range is the operation BGP RTBH is actually built for, so this
385 # is the more natural of the two paths even though the single address one sees
386 # more use. The driver handling is identical to _route_command and the same
387 # notes about frr, gobgp, and the exabgp quoting apply.
388 #
389 # There are two differences from _route_command, both following from the
390 # argument already being a prefix. No mask is appended, since the range
391 # carries its own prefix length, and the family has to be read off the address
392 # portion rather than the whole string, because a range always holds a "/"
393 # that would never match the IPv4 regexp. A value with no "/" at all is used
394 # as is and matched for family directly, so passing a bare address here
395 # degrades to announcing it without a prefix length rather than failing.
396 #
397 # Args:
398 #
399 # verb - What to do, as a plain string: 'announce' to install the
400 # blackhole or 'withdraw' to remove it. Mapped onto each driver's
401 # own spelling exactly as in _route_command.
402 #
403 # cidr - The range to blackhole, as a plain string. Expected to be an
404 # already validated CIDR range such as "10.0.0.0/8" or
405 # "2001:db8::/32". Announced verbatim, so the prefix length given
406 # is the one that goes out.
407 #
408 # Returns the command as a single string ready to hand to the runner.
409 #
410 # # driver exabgp, announce_type rtbh
411 # $self->_route_command_cidr( 'announce', '10.0.0.0/8' );
412 # # exabgpcli 'announce route 10.0.0.0/8 next-hop 192.0.2.1 community [65535:666]'
413 #
414 # # driver exabgp, announce_type flowspec
415 # # exabgpcli 'announce flow route { match { source 10.0.0.0/8; } then { discard; } }'
416 #
417 # # driver gobgp
418 # # gobgp global rib add 10.0.0.0/8 nexthop 192.0.2.1 community 65535:666 -a ipv4
419 #
420 # # driver frr, IPv6 range
421 # # vtysh -c 'configure terminal' -c 'ipv6 route 2001:db8::/32 blackhole'
422 3 ▶sub _route_command_cidr {
Subroutine _route_command_cidr: called CC=19 SCAR=46.4
423 3 my ( $self, $verb, $cidr ) = @_;
424  
425 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
426 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
427 3 ▶ my $nh = $is_v4 ? $self->{options}{next_hop} : $self->{options}{next_hop6};
Branch: 1 branch
BranchTrueFalseTotal
$is_v4 ? : 2 1 3
428 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
429 3 my $prefix = $cidr;
430 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
431  
432 3 ▶ if ( $self->{options}{driver} eq 'frr' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"driver"} eq "frr") 0 3 3
433 # frr injects a blackhole static route via vtysh; a redistribute-static
434 # route-map on the router tags it with the blackhole community for BGP
435 0 ▶ my $keyword = $is_v4 ? 'ip route' : 'ipv6 route';
Branch: 1 branch
BranchTrueFalseTotal
$is_v4 ? : 0 0 0
436 0 ▶ my $line = ( $verb eq 'announce' ? '' : 'no ' ) . $keyword . ' ' . $prefix . ' blackhole';
Branch: 1 branch
BranchTrueFalseTotal
$verb eq 'announce' ? : 0 0 0
437 0 return $self->{options}{vtysh_cmd} . " -c 'configure terminal' -c '" . $line . "'";
438 }
439  
440 3 ▶ if ( $self->{options}{driver} eq 'gobgp' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"driver"} eq "gobgp") 0 3 3
441 0 ▶ my $op = ( $verb eq 'announce' ) ? 'add' : 'del';
Branch: 1 branch
BranchTrueFalseTotal
$verb eq 'announce' ? : 0 0 0
442  
443 0 ▶ if ($is_flowspec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_flowspec) 0 0 0
444 # gobgp global rib add|del -a ipv4-flowspec match source <prefix> then discard
445 0 ▶ my $afi = $is_v4 ? 'ipv4-flowspec' : 'ipv6-flowspec';
Branch: 1 branch
BranchTrueFalseTotal
$is_v4 ? : 0 0 0
446 return
447 $self->{options}{gobgp_cmd}
448 0 . ' global rib '
449 . $op
450 . ' -a '
451 . $afi
452 . ' match source '
453 . $prefix
454 . ' then discard';
455 } ## end if ($is_flowspec)
456  
457 # gobgp global rib add|del <prefix> [nexthop ..] [community ..] -a <afi>
458 0 ▶ my $afi = $is_v4 ? 'ipv4' : 'ipv6';
Branch: 1 branch
BranchTrueFalseTotal
$is_v4 ? : 0 0 0
459 0 my $cmd = $self->{options}{gobgp_cmd} . ' global rib ' . $op . ' ' . $prefix;
460  
461 # a withdrawal matches on prefix alone; attributes are only needed to add
462 0 ▶ if ( $op eq 'add' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "add") 0 0 0
463 0 $cmd .= ' nexthop ' . $nh . ' community ' . $self->{options}{community};
464 0 ▶ $cmd .= ' ' . $extra if ( $extra ne '' );
Branch: 1 branch
BranchTrueFalseTotal
if $extra ne "" 0 0 0
465 }
466 0 $cmd .= ' -a ' . $afi;
467  
468 0 return $cmd;
469 } ## end if ( $self->{options}{driver...})
470  
471 # exabgp driver; the command is passed single quoted as the braces and
472 # semicolons of the flow syntax are shell syntax when left bare
473 3 ▶ if ($is_flowspec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_flowspec) 0 3 3
474 # exabgpcli 'announce|withdraw flow route { match { source <prefix>; } then { discard; } }'
475 return
476 0 $self->{options}{exabgpcli_cmd} . " '"
477 . $verb
478 . ' flow route { match { source '
479 . $prefix
480 . "; } then { discard; } }'";
481 } ## end if ($is_flowspec)
482  
483 # exabgp rtbh: exabgpcli 'announce|withdraw route <prefix> next-hop .. community [..]'
484 my $route
485 = $verb
486 . ' route '
487 . $prefix
488 . ' next-hop '
489 . $nh
490 3 . ' community [' . $self->{options}{community} . ']';
491 3 ▶ $route .= ' ' . $extra if ( $extra ne '' );
Branch: 1 branch
BranchTrueFalseTotal
if $extra ne "" 0 3 3
492  
493 # single quoted so the community's [..] can not glob against the cwd
494 3 return $self->{options}{exabgpcli_cmd} . " '" . $route . "'";
495 } ## end sub _route_command_cidr
496  
497 # Internal helper. Returns the command check uses to confirm the BGP daemon is
498 # still reachable, in whichever driver's syntax is configured.
499 #
500 # Unlike the packet filter backends there is nothing of this instance's own to
501 # look for. The BGP session belongs to the running daemon, which this backend
502 # only talks to, and announcements live in that daemon's RIB rather than in
503 # any object created at init. What can actually go wrong is the daemon being
504 # restarted or going away, taking every announcement with it, so reachability
505 # is the meaningful health check and is what triggers a re_init and
506 # re-announcement of the current bans.
507 #
508 # All three commands are read only queries chosen because they fail cleanly
509 # when the daemon is not there, rather than because their output is
510 # interesting. Only the exit status is looked at; nothing parses what comes
511 # back.
512 #
513 # Takes no arguments; the driver and the command paths come from the options.
514 #
515 # Returns the command as a single string ready to hand to the runner, whose
516 # exit status is 0 when the daemon answers.
517 #
518 # # driver exabgp, the default
519 # $self->_check_command; # exabgpcli show neighbor summary
520 #
521 # # driver gobgp
522 # $self->_check_command; # gobgp neighbor
523 #
524 # # driver frr
525 # $self->_check_command; # vtysh -c 'show ip bgp summary'
526 19 ▶sub _check_command {
Subroutine _check_command: called CC=3 SCAR=11.0
527 19 my ($self) = @_;
528  
529 19 ▶ if ( $self->{options}{driver} eq 'frr' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"driver"} eq "frr") 3 16 19
530 3 return $self->{options}{vtysh_cmd} . " -c 'show ip bgp summary'";
531 }
532 16 ▶ if ( $self->{options}{driver} eq 'gobgp' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"driver"} eq "gobgp") 5 11 16
533 5 return $self->{options}{gobgp_cmd} . ' neighbor';
534 }
535  
536 11 return $self->{options}{exabgpcli_cmd} . ' show neighbor summary';
537 } ## end sub _check_command
538  
539 =head2 init
540  
541 Initiates the backend. The BGP session is owned by the running BGP daemon
542 (exabgp, gobgpd, or FRR), so there is nothing to set up; this only flips the
543 inited flag.
544  
545 =cut
546  
547 7 ▶sub init {
Subroutine init: called CC=3 SCAR=12.6
Pod documented
548 7 my ( $self, %opts ) = @_;
549  
550 7 $self->errorblank;
551  
552 7 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 7 7
553 0 $self->{error} = 18;
554 0 $self->{errorString} = 'backend has already been inited';
555 0 $self->warn;
556 }
557  
558 7 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 7 0 7
559 7 $self->{frontend_obj}->{test_data} = [];
560 }
561  
562 7 $self->{inited} = 1;
563 } ## end sub init
564  
565 =head2 ban
566  
567 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address
568 and lowercased, then announced via the configured driver: a
569 C<exabgpcli announce route> or C<gobgp global rib add> of the host route
570 (C<mask4>/C<mask6> prefix length) with the family-appropriate next hop and
571 the blackhole community, a FlowSpec discard rule when announce_type is
572 C<flowspec>, or a C<vtysh> injected blackhole static route for the frr
573 driver. Banning an already banned IP is a noop.
574  
575 $fw_helper->ban( ban => $ip );
576  
577 =cut
578  
579 12 ▶sub ban {
Subroutine ban: called CC=10 SCAR=33.4
Pod documented
580 12 my ( $self, %opts ) = @_;
581  
582 12 $self->errorblank;
583  
584 12 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 12 12
585 0 $self->{error} = 1;
586 0 $self->{errorString} = 'backend has not been inited';
587 0 $self->warn;
588 0 return;
589 }
590  
591 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
592 0 $self->{error} = 9;
593 0 $self->{errorString} = 'Nothing specified for the value ban';
594 0 $self->warn;
595 0 return;
596 } elsif ( ref( $opts{ban} ) ne '' ) {
597 0 $self->{error} = 10;
598 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
599 0 $self->warn;
600 0 return;
601 ▶ } 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
602 && $opts{ban} !~ /\A$IPv6_re\z/ )
603 {
604 0 $self->{error} = 10;
605 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
606 0 $self->warn;
607 0 return;
608 }
609  
610 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
611 12 $opts{ban} = lc( $opts{ban} );
612  
613 12 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 1 11 12
614 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 1 0 1
615 1 $self->{frontend_obj}->{test_data} = 'already banned';
616 }
617 1 return;
618 }
619  
620 11 my $command = $self->_route_command( 'announce', $opts{ban} );
621  
622 11 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 11 0 11
623 11 $self->{frontend_obj}->{test_data} = $command;
624 } else {
625 0 my $output = `$command 2>&1`;
626 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
627 0 $self->{error} = 13;
628 $self->{errorString}
629 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
630 0 $self->warn;
631 }
632 }
633  
634 11 $self->{banned}{ $opts{ban} } = 1;
635 } ## end sub ban
636  
637 =head2 unban
638  
639 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
640 and lowercased, then the route announced for it by L</ban> is withdrawn via
641 the configured driver (C<exabgpcli withdraw>, C<gobgp global rib del>, or a
642 C<vtysh> C<no ... blackhole> for frr). Unbanning an IP that is not banned is
643 a noop.
644  
645 $fw_helper->unban( ban => $ip );
646  
647 =cut
648  
649 3 ▶sub unban {
Subroutine unban: called CC=10 SCAR=37.2
Pod documented
650 3 my ( $self, %opts ) = @_;
651  
652 3 $self->errorblank;
653  
654 3 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 3 3
655 0 $self->{error} = 1;
656 0 $self->{errorString} = 'backend has not been inited';
657 0 $self->warn;
658 0 return;
659 }
660  
661 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
662 0 $self->{error} = 9;
663 0 $self->{errorString} = 'Nothing specified for the value ban';
664 0 $self->warn;
665 0 return;
666 } elsif ( ref( $opts{ban} ) ne '' ) {
667 0 $self->{error} = 10;
668 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
669 0 $self->warn;
670 0 return;
671 ▶ } 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
672 && $opts{ban} !~ /\A$IPv6_re\z/ )
673 {
674 0 $self->{error} = 10;
675 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
676 0 $self->warn;
677 0 return;
678 }
679  
680 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
681 3 $opts{ban} = lc( $opts{ban} );
682  
683 3 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 3 3
684 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
685 0 $self->{frontend_obj}->{test_data} = 'not banned';
686 }
687 0 return;
688 }
689  
690 3 my $command = $self->_route_command( 'withdraw', $opts{ban} );
691  
692 3 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 3 0 3
693 3 $self->{frontend_obj}->{test_data} = $command;
694 } else {
695 0 my $output = `$command 2>&1`;
696 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
697 0 $self->{error} = 14;
698 $self->{errorString}
699 0 = 'unban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
700 0 $self->warn;
701 }
702 }
703  
704 3 delete( $self->{banned}{ $opts{ban} } );
705 } ## end sub unban
706  
707 =head2 ban_cidr
708  
709 Bans a CIDR range. The value of ban is validated as being a IPv4 or IPv6
710 CIDR range and lowercased, then announced the same way L</ban> announces a
711 single IP, except the range is announced verbatim rather than having the
712 C<mask4>/C<mask6> host prefix length appended. Banning an already banned
713 range is a noop.
714  
715 $fw_helper->ban_cidr( ban => '1.2.3.0/24' );
716  
717 =cut
718  
719 2 ▶sub ban_cidr {
Subroutine ban_cidr: called CC=9 SCAR=35.4
Pod documented
720 2 my ( $self, %opts ) = @_;
721  
722 2 $self->errorblank;
723  
724 2 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 2 2
725 0 $self->{error} = 1;
726 0 $self->{errorString} = 'backend has not been inited';
727 0 $self->warn;
728 0 return;
729 }
730  
731 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
732 0 $self->{error} = 9;
733 0 $self->{errorString} = 'Nothing specified for the value ban';
734 0 $self->warn;
735 0 return;
736 } elsif ( ref( $opts{ban} ) ne '' ) {
737 0 $self->{error} = 28;
738 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
739 0 $self->warn;
740 0 return;
741 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
742 0 $self->{error} = 28;
743 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
744 0 $self->warn;
745 0 return;
746 }
747  
748 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
749 2 $opts{ban} = lc( $opts{ban} );
750  
751 2 ▶ if ( $self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned_cidr"}{$opts{"ban"}}) 0 2 2
752 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
753 0 $self->{frontend_obj}->{test_data} = 'already banned';
754 }
755 0 return;
756 }
757  
758 2 my $command = $self->_route_command_cidr( 'announce', $opts{ban} );
759  
760 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
761 2 $self->{frontend_obj}->{test_data} = $command;
762 } else {
763 0 my $output = `$command 2>&1`;
764 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
765 0 $self->{error} = 26;
766 $self->{errorString}
767 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
768 0 $self->warn;
769 }
770 }
771  
772 2 $self->{banned_cidr}{ $opts{ban} } = 1;
773 } ## end sub ban_cidr
774  
775 =head2 unban_cidr
776  
777 Unbans a CIDR range. The value of ban is validated as being a IPv4 or IPv6
778 CIDR range and lowercased, then the route announced for it by L</ban_cidr>
779 is withdrawn via the configured driver. Unbanning a range that is not banned
780 is a noop.
781  
782 $fw_helper->unban_cidr( ban => '1.2.3.0/24' );
783  
784 =cut
785  
786 1 ▶sub unban_cidr {
Subroutine unban_cidr: called CC=9 SCAR=35.4
Pod documented
787 1 my ( $self, %opts ) = @_;
788  
789 1 $self->errorblank;
790  
791 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
792 0 $self->{error} = 1;
793 0 $self->{errorString} = 'backend has not been inited';
794 0 $self->warn;
795 0 return;
796 }
797  
798 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
799 0 $self->{error} = 9;
800 0 $self->{errorString} = 'Nothing specified for the value ban';
801 0 $self->warn;
802 0 return;
803 } elsif ( ref( $opts{ban} ) ne '' ) {
804 0 $self->{error} = 28;
805 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
806 0 $self->warn;
807 0 return;
808 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
809 0 $self->{error} = 28;
810 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
811 0 $self->warn;
812 0 return;
813 }
814  
815 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
816 1 $opts{ban} = lc( $opts{ban} );
817  
818 1 ▶ if ( !$self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
819 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
820 0 $self->{frontend_obj}->{test_data} = 'not banned';
821 }
822 0 return;
823 }
824  
825 1 my $command = $self->_route_command_cidr( 'withdraw', $opts{ban} );
826  
827 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
828 1 $self->{frontend_obj}->{test_data} = $command;
829 } else {
830 0 my $output = `$command 2>&1`;
831 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
832 0 $self->{error} = 27;
833 $self->{errorString}
834 0 = 'unban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
835 0 $self->warn;
836 }
837 }
838  
839 1 delete( $self->{banned_cidr}{ $opts{ban} } );
840 } ## end sub unban_cidr
841  
842 =head2 list_cidr
843  
844 List banned CIDR ranges. Returns an array of the currently banned ranges
845 from internal state; the BGP daemon is not queried.
846  
847 my @banned_cidrs = $fw_helper->list_cidr;
848  
849 =cut
850  
851 2 ▶sub list_cidr {
Subroutine list_cidr: called CC=2 SCAR=7.0
Pod documented
852 2 my ( $self, %opts ) = @_;
853  
854 2 $self->errorblank;
855  
856 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
857 2 $self->{frontend_obj}->{test_data} = 'list_cidr';
858 }
859  
860 2 return keys( %{ $self->{banned_cidr} } );
861 }
862  
863 =head2 list
864  
865 List banned IPs. Returns an array of the currently banned single IPs from
866 internal state; the BGP daemon is not queried. CIDR ranges are not included;
867 for those see L</list_cidr>.
868  
869 my @banned = $fw_helper->list;
870  
871 =cut
872  
873 1 ▶sub list {
Subroutine list: called CC=2 SCAR=7.0
Pod documented
874 1 my ( $self, %opts ) = @_;
875  
876 1 $self->errorblank;
877  
878 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 1 0 1
879 1 $self->{frontend_obj}->{test_data} = 'list';
880 }
881  
882 1 return keys( %{ $self->{banned} } );
883 }
884  
885 =head2 re_init
886  
887 Re-announces every retained blackhole route. A best effort teardown
888 (failures ignored) is done first, then the announce command is re-run for
889 every banned IP and CIDR range. The BGP daemons do not persist announced
890 state across restarts, so this is how the announcements are restored after
891 the daemon is bounced.
892  
893 =cut
894  
895 0 ▶sub re_init {
Subroutine re_init: not called CC=8 SCAR=42.8
Pod documented
896 0 my ( $self, %opts ) = @_;
897  
898 0 $self->errorblank;
899  
900 # teardown is best effort; a session that already dropped the routes is
901 # exactly what re_init is meant to recover from
902 {
903 0 local $@;
904 0 eval { $self->teardown; };
905 }
906 0 $self->init;
907  
908 0 my @re_init_test_data;
909 0 foreach my $item ( keys( %{ $self->{banned} } ) ) {
910 0 my $command = $self->_route_command( 'announce', $item );
911 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
912 0 push( @re_init_test_data, $command );
913 } else {
914 0 my $output = `$command 2>&1`;
915 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
916 0 $self->{error} = 13;
917 $self->{errorString}
918 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
919 0 $self->warn;
920 }
921 }
922 } ## end foreach my $item ( keys( %{ ...}))
923  
924 0 foreach my $item ( keys( %{ $self->{banned_cidr} } ) ) {
925 0 my $command = $self->_route_command_cidr( 'announce', $item );
926 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
927 0 push( @re_init_test_data, $command );
928 } else {
929 0 my $output = `$command 2>&1`;
930 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
931 0 $self->{error} = 26;
932 $self->{errorString}
933 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
934 0 $self->warn;
935 }
936 }
937 } ## end foreach my $item ( keys( %{ ...}))
938  
939 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
940 0 $self->{frontend_obj}->{test_data} = \@re_init_test_data;
941 }
942  
943 0 $self->{inited} = 1;
944 } ## end sub re_init
945  
946 =head2 teardown
947  
948 Withdraws every announced blackhole route, running the driver's withdraw
949 command for each banned IP and CIDR range, and marks the backend as not
950 inited. The ban list is retained, so L</re_init> can restore the
951 announcements.
952  
953 =cut
954  
955 1 ▶sub teardown {
Subroutine teardown: called CC=6 SCAR=22.4
Pod documented
956 1 my ( $self, %opts ) = @_;
957  
958 1 $self->errorblank;
959  
960 1 $self->{inited} = 0;
961  
962 1 my @commands;
963 1 foreach my $item ( keys( %{ $self->{banned} } ) ) {
964 2 push( @commands, $self->_route_command( 'withdraw', $item ) );
965 }
966 1 foreach my $item ( keys( %{ $self->{banned_cidr} } ) ) {
967 0 push( @commands, $self->_route_command_cidr( 'withdraw', $item ) );
968 }
969  
970 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
971 1 $self->{frontend_obj}->{test_data} = \@commands;
972 } else {
973 0 foreach my $item (@commands) {
974 0 my $output = `$item 2>&1`;
975 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
976 0 $self->{error} = 17;
977 $self->{errorString}
978 0 = 'teardown failed. non-zero exit code for the command... "' . $item . '"... output... ' . $output;
979 0 $self->warn;
980 }
981 }
982 } ## end else [ if ( $self->{testing} ) ]
983 } ## end sub teardown
984  
985 =head2 stop
986  
987 Alias for L</teardown>.
988  
989 =cut
990  
991 0 ▶sub stop {
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
992 0 my ( $self, %opts ) = @_;
993  
994 0 return $self->teardown(%opts);
995 }
996  
997 =head2 check
998  
999 Runs the driver's neighbor summary command (C<exabgpcli show neighbor
1000 summary>, C<gobgp neighbor>, or C<vtysh -c 'show ip bgp summary'> for frr)
1001 and treats a zero exit as healthy, returning 1, and 0 otherwise. This only
1002 confirms the BGP daemon holding the announcements is reachable; whether the
1003 sessions to the routers are up and the announcements accepted is not
1004 checked.
1005  
1006 =cut
1007  
1008 19 ▶sub check {
Subroutine check: called CC=3 SCAR=13.0
Pod documented
1009 19 my ( $self, %opts ) = @_;
1010  
1011 19 $self->errorblank;
1012  
1013 19 my $command = $self->_check_command;
1014  
1015 19 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 19 0 19
1016 19 $self->{frontend_obj}->{test_data} = $command;
1017 19 return 1;
1018 }
1019  
1020 0 my $output = `$command 2>&1`;
1021 0 ▶ return $? == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$? == 0 ? : 0 0 0
1022 } ## end sub check
1023  
1024 =head2 flush
1025  
1026 Removes all bans at once by running the driver's withdraw command for every
1027 banned IP and CIDR range, then clearing the ban lists.
1028  
1029 =cut
1030  
1031 0 ▶sub flush {
Subroutine flush: not called CC=7 SCAR=40.3
Pod documented
1032 0 my ( $self, %opts ) = @_;
1033  
1034 0 $self->errorblank;
1035  
1036 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
1037 0 $self->{error} = 1;
1038 0 $self->{errorString} = 'backend has not been inited';
1039 0 $self->warn;
1040 0 return;
1041 }
1042  
1043 0 my @commands;
1044 0 foreach my $item ( keys( %{ $self->{banned} } ) ) {
1045 0 push( @commands, $self->_route_command( 'withdraw', $item ) );
1046 }
1047 0 foreach my $item ( keys( %{ $self->{banned_cidr} } ) ) {
1048 0 push( @commands, $self->_route_command_cidr( 'withdraw', $item ) );
1049 }
1050  
1051 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
1052 0 $self->{frontend_obj}->{test_data} = \@commands;
1053 } else {
1054 0 foreach my $item (@commands) {
1055 0 my $output = `$item 2>&1`;
1056 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
1057 0 $self->{error} = 25;
1058 $self->{errorString}
1059 0 = 'flush failed. non-zero exit code for the command... "' . $item . '"... output... ' . $output;
1060 0 $self->warn;
1061 }
1062 }
1063 } ## end else [ if ( $self->{testing} ) ]
1064  
1065 0 $self->{banned} = {};
1066 0 $self->{banned_cidr} = {};
1067 } ## end sub flush
1068  
1069 =head1 ERROR CODES / FLAGS
1070  
1071 Error handling is provided by L<Error::Helper>. All
1072 errors are considered fatal.
1073  
1074 =head2 1, notInited
1075  
1076 The backend has not been inited yet.
1077  
1078 =head2 8, optionsNotHash
1079  
1080 The item passed to new for options is not a hash.
1081  
1082 =head2 9, noBanItem
1083  
1084 No IP or CIDR range specified to ban or unban.
1085  
1086 =head2 10, banItemNotIP
1087  
1088 The item to ban is not an IP. Either wrong ref type or regexp
1089 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
1090  
1091 =head2 12, backendInitError
1092  
1093 Failed to init the backend.
1094  
1095 =head2 13, banFailed
1096  
1097 Failed to ban the item.
1098  
1099 =head2 14, unbanFailed
1100  
1101 Failed to unban the item.
1102  
1103 =head2 15, listFailed
1104  
1105 Failed to get a list of bans.
1106  
1107 =head2 16, reInitFailed
1108  
1109 Failed to re_init the backend.
1110  
1111 =head2 17, teardownFailed
1112  
1113 Failed to teardown the backend.
1114  
1115 =head2 18, alreadyInited
1116  
1117 init called, but the backend has already been inited.
1118  
1119 =head2 20, driverInvalid
1120  
1121 The driver option is not one of 'exabgp', 'gobgp', or 'frr'.
1122  
1123 =head2 21, announceTypeInvalid
1124  
1125 The announce_type is not 'rtbh' or 'flowspec', or flowspec was requested with
1126 the frr driver, which does not support it.
1127  
1128 =head2 24, checkFailed
1129  
1130 The backend check raised an error.
1131  
1132 =head2 25, flushFailed
1133  
1134 Failed to flush the bans.
1135  
1136 =head2 26, banCidrFailed
1137  
1138 Failed to ban the CIDR range.
1139  
1140 =head2 27, unbanCidrFailed
1141  
1142 Failed to unban the CIDR range.
1143  
1144 =head2 28, cidrItemNotCidr
1145  
1146 The item to ban is not a CIDR range. Either wrong ref type or it is not an
1147 IPv4 or IPv6 address followed by a prefix length valid for its family.
1148  
1149 =head2 29, cidrNotSupported
1150  
1151 The backend does not support CIDR bans.
1152  
1153 =head2 30, listCidrFailed
1154  
1155 Failed to get a list of CIDR bans.
1156  
1157 =head1 AUTHOR
1158  
1159 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
1160  
1161 =head1 LICENSE AND COPYRIGHT
1162  
1163 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
1164  
1165 This is free software, licensed under:
1166  
1167 The GNU Lesser General Public License, Version 2.1, February 1999
1168  
1169 =cut
1170  
1171 1 1; # End of Net::Firewall::BlockerHelper::backends::bgp_rtbh
« BlockerHelper/backends/abuseipdb.pm ↑ Summary BlockerHelper/backends/nsupdate.pm »