Documentation
¶
Overview ¶
Package nft enforces platform bans in the kernel with nftables.
The agent owns exactly one table, `table inet edgeweir`:
set ban4 / ban6 flags interval, timeout: banned prefixes, each
element with the seconds left until it expires
set allow4 / allow6 flags interval: addresses that are never dropped
(console, the node's own addresses, loopback,
platform allow lists)
chain input type filter hook input priority -10; policy accept;
allow sets accept, ban sets drop, new TCP
connections to @limitports jump to connlimit
set limitports flags interval: TCP ports with connection limits
set conn4 / conn6 flags dynamic: connections per client (ct count)
set rate4 / rate6 flags dynamic, timeout: new connections per client
(limit rate)
chain connlimit the connection limits of the applied configuration
(empty without limits); a client is an IPv4
address or an IPv6 /64
Every change is one `nft -f -` transaction (flush the sets, add the elements), so the kernel never sees a half-written set. Interval sets refuse overlapping elements: a prefix covered by a larger banned prefix that expires no earlier is left out, and one that outlives its cover is written once the cover has expired (Sync returns when to sync again).
Connection limits are written in the same transaction (flush the chain, add its rules); when nftables refuses them (a kernel without nft_connlimit) the bans are written without them and the error is kept for the node's status.
Scripts are built only from netip values formatted by the standard library and integers; no string from the console reaches nft unparsed.
Index ¶
- Constants
- Variables
- type Ban
- type Command
- type Executor
- type Fake
- type Limits
- type Manager
- func (m *Manager) Active() bool
- func (m *Manager) Entries(now time.Time) int
- func (m *Manager) LimitsStatus() (bool, string)
- func (m *Manager) Start(ctx context.Context) error
- func (m *Manager) Stop(ctx context.Context) error
- func (m *Manager) Sync(ctx context.Context, bans []Ban, protected []netip.Prefix, limits *Limits, ...) (time.Time, error)
- type PortRange
Constants ¶
const SetupScript = `` /* 887-byte string literal not displayed */
SetupScript (re)creates the table: `add table` followed by `delete table` removes whatever an earlier run left behind without failing when there is nothing (nft 1.0.6 has no `destroy`), then the table is defined from scratch.
const StopScript = "delete table inet edgeweir\n"
StopScript removes the table.
const Table = "inet edgeweir"
Table is the family and name of the agent's table.
Variables ¶
var ErrInactive = errors.New("kernel bans are not active")
ErrInactive is returned by Sync when the table is not managed.
Functions ¶
This section is empty.
Types ¶
type Command ¶
type Command struct {
Bin string // default "nft"
}
Command runs `<Bin> -f -` with the script on standard input.
type Fake ¶
type Fake struct {
// Fail, when set, decides whether a script fails (and with what).
Fail func(script string) error
// contains filtered or unexported fields
}
Fake is an Executor for tests: it records every script and fails the scripts Fail selects.
type Limits ¶ added in v1.6.0
Limits are per-client TCP connection limits (NodeConfig.connection_limits, validated by configir): PerIP concurrent connections and NewPerSecond new connections per second with Burst, each 0 when off, on Ports.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager owns the table. It is safe for concurrent use.
func NewManager ¶
NewManager returns an inactive manager.
func (*Manager) LimitsStatus ¶ added in v1.6.0
LimitsStatus reports whether connection limits are written and why the last ones asked for are not (empty when they are, or none were asked for).
func (*Manager) Start ¶
Start creates the table. An error means nftables cannot be used (no nft binary, no CAP_NET_ADMIN, no nf_tables in the kernel): the manager stays inactive and bans are enforced at the edge layer only.
func (*Manager) Sync ¶
func (m *Manager) Sync(ctx context.Context, bans []Ban, protected []netip.Prefix, limits *Limits, now time.Time) (time.Time, error)
Sync replaces the sets with bans and protected prefixes and the connection limits with limits (nil: none). When the sets cannot be written (for example the table was deleted by someone else) the table is created again once and the sync retried; when only the limits are refused, the bans are written without them (LimitsStatus). It returns when a ban left out because of an overlap must be written (zero: never).