NAME
Net::Firewall::BlockerHelper::backends::nftables - nftables backend for Net::Firewall::BlockerHelper.
VERSION
Version 0.1.0
SYNOPSIS
use Net::Firewall::BlockerHelper;
my $fw_helper = Net::Firewall::BlockerHelper->new(
backend => 'nftables',
ports => ['22'],
protocols => ['tcp'],
name => 'ssh',
);
$fw_helper->init_backend;
$fw_helper->ban(ban => '1.2.3.4');
$fw_helper->ban(ban => 'dead::1');
$fw_helper->unban(ban => '1.2.3.4');
$fw_helper->teardown;
DESCRIPTION
Blocks IPs using nft(8).
A dedicated table inet <prefix>_<name> is created per instance, containing a base chain hooked to input, one set for IPv4 (<prefix>_<name>_4) and one for IPv6 (<prefix>_<name>_6), and the block rules referencing them. Banning an IP adds it to the relevant set. As the whole setup lives in its own table, teardown is simply deleting the table.
Requires nft to be installed and in the PATH of the process, which must have sufficient privileges to run it.
METHODS
new
Initiates the object.
- options :: Backend specific options that will be passed to the backend unchecked
outside of making sure it is a hash ref if defined. See below for furhter info.
- Default :: {}
- ports :: A array of ports to block. Checked to make sure they are ints within the
range 1 to 65535 or a valid service name via getservbyname. All ports will be
blocked if non are specified. Duplicates are removed.
- Default :: []
- protocols :: A array of protocols to block. This is checked against
/etc/protocols via the function getprotobyname. Duplicates will be
discarded. If no protocols are given, all IPv4 and IPv6 traffic
sourced from the sets is blocked, unless ports are given, in which
case it defaults to tcp and udp. Ports are only attached to
port-capable protocols (tcp/udp/sctp); other protocols are blocked
without a port.
- Default :: [], or ['tcp','udp'] when ports are given
- prefix :: Prefix to use. Must match the regex /^[a-zA-Z0-9]+$/
- default :: kur
- name :: Name of this specific instance. This must be specified.
- default :: undef
The options hash accepts the following.
- type :: The drop method to use. 'drop' silently drops. 'reject' sends
the family-appropriate ICMP port-unreachable back.
- Default :: drop
- priority :: Priority of the base chain. See nft(8).
- Default :: -1
- kill :: Use conntrack(8) to drop existing connection tracking entries
for the banned IP, for both IPv4 and IPv6. When protocols are
configured the kill is scoped to them via -p, so blocking only
udp will not drop tcp entries and vice versa; protocols conntrack
can not filter by are skipped. With no protocols configured
everything is being blocked, so entries of every protocol are
dropped.
- Default :: 0
All errors are considered fatal, meaning if new fails it will die.
init
Initiates the backend. First a best effort nft delete table is run to clear any stale copy of the table, then the table inet <prefix>_<name>, a base chain of the same name hooked to input at the configured priority, the IPv4 set (<prefix>_<name>_4), the IPv6 set (<prefix>_<name>_6), and the block rules referencing the sets are created via nft add. A failure of any of the creation commands raises an error.
$backend->init;
ban
Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address and lowercased, then added to the family-appropriate set via nft add element. If the kill option is set, conntrack(8) is then used to drop existing connection tracking entries for the IP. Banning an already banned IP is a noop.
$backend->ban(ban => $ip);
unban
Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address and lowercased, then removed from the family-appropriate set via nft delete element. Unbanning an IP that is not banned is a noop.
$backend->unban(ban => $ip);
list
List banned IPs. Returns an array of the currently banned IPs from the retained in-memory state; the sets are not queried.
my @banned = $backend->list;
re_init
Tears down and re-initiates the backend, recreating the table, chain, sets, and rules, then re-adds every previously banned IP to the relevant set via nft add element. The teardown is best effort as a partially or fully wiped setup is exactly what this needs to recover from.
$backend->re_init;
teardown
Tears down the setup for the backend by deleting the table, which removes the chain, sets, and rules with it.
$backend->teardown;
stop
Alias for "teardown", provided for parity with the fail2ban actionstop concept.
$backend->stop;
check
Verifies that the table is still present and that the chain still contains rules referencing each set the block rules use. Returns a true value if the setup is intact and a false value if any part of it appears to have been removed. This is the equivalent of fail2ban's actioncheck.
if ( !$backend->check ) {
$backend->re_init;
}
flush
Removes all currently banned IPs at once by flushing both sets, leaving the table, chain, and rules in place. This is the equivalent of fail2ban's actionflush.
$backend->flush;
ban_cidr
CIDR bans are not supported by this backend; this always sets the cidrNotSupported error.
$backend->ban_cidr(ban => '1.2.3.0/24');
unban_cidr
CIDR bans are not supported by this backend; this always sets the cidrNotSupported error.
$backend->unban_cidr(ban => '1.2.3.0/24');
list_cidr
CIDR bans are not supported by this backend, so this always returns an empty list.
my @banned_cidrs = $backend->list_cidr;
ERROR CODES / FLAGS
Error handling is provided by Error::Helper. All errors are considered fatal.
1, notInited
The backend has not been inited yet.
2, invalidPortSpecified
Port is either not an int within the range 1 to 65535 or a name that can be resolved by getservbyname.
3, portsNotArray
The data passed to new for ports is not an array.
4, protocolsNotArray
The data passed to new for protocols is not an array.
5, invalidPortSpecified
Port is either not an int within the range 1 to 65535 or a name that can be resolved by getservbyname.
6, invalidPrefixSpecified
The specified prefix did not match /^[a-zA-Z0-9]+$/.
7, invalidName
The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
8, optionsNotHash
The item passed to new for options is not a hash.
9, noBanItem
No IP specified to ban or unban.
10, banItemNotIP
The item to ban is not an IP. Either wrong ref type or regexp test using Regexp::IPv4 and Regexp::IPv6 failed.
12, backendInitError
Failed to init the backend.
13, banFailed
Failed to ban the item.
14, unbanFailed
Failed to unban the item.
15, listFailed
Failed to get a list of bans.
16, reInitFailed
Failed to re_init the backend.
17, teardownFailed
Failed to teardown the backend.
18, alreadyInited
init called, but the backend has already been inited.
19, priorityInvalid
The option priority is not an int.
20, typeInvalid
The option type is not "drop" or "reject".
23, initFailed
One of the required commands for init failed.
24, checkFailed
The backend check raised an error.
25, flushFailed
Failed to flush the bans.
26, banCidrFailed
Failed to ban the CIDR range.
27, unbanCidrFailed
Failed to unban the CIDR range.
28, cidrItemNotCidr
The item to ban is not a CIDR range. Either wrong ref type or it is not an IPv4 or IPv6 address followed by a prefix length valid for its family.
29, cidrNotSupported
The backend does not support CIDR bans.
30, listCidrFailed
Failed to get a list of CIDR bans.
AUTHOR
Zane C. Bowers-Hadley, <vvelox at vvelox.ent>
BUGS
Please report any bugs or feature requests to bug-net-firewall-blockerhelper at rt.cpan.org, or through the web interface at https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Firewall-BlockerHelper.
LICENSE AND COPYRIGHT
This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
This is free software, licensed under:
The GNU Lesser General Public License, Version 2.1, February 1999