nft

package
v1.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 19, 2026 License: AGPL-3.0 Imports: 14 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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.

View Source
const StopScript = "delete table inet edgeweir\n"

StopScript removes the table.

View Source
const Table = "inet edgeweir"

Table is the family and name of the agent's table.

Variables

View Source
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 Ban

type Ban struct {
	Prefix  netip.Prefix
	Expires time.Time
}

Ban is a banned prefix until Expires.

type Command

type Command struct {
	Bin string // default "nft"
}

Command runs `<Bin> -f -` with the script on standard input.

func (Command) Run

func (c Command) Run(ctx context.Context, script string) error

Run implements Executor.

type Executor

type Executor interface {
	Run(ctx context.Context, script string) error
}

Executor runs an nft script as one transaction.

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.

func (*Fake) Last

func (f *Fake) Last(substr string) string

Last returns the last script whose text contains substr ("" if none).

func (*Fake) Run

func (f *Fake) Run(_ context.Context, script string) error

Run implements Executor.

func (*Fake) Scripts

func (f *Fake) Scripts() []string

Scripts returns the scripts run so far.

type Limits added in v1.6.0

type Limits struct {
	PerIP        uint32
	NewPerSecond uint32
	Burst        uint32
	Ports        []PortRange
}

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

func NewManager(exec Executor, log *slog.Logger) *Manager

NewManager returns an inactive manager.

func (*Manager) Active

func (m *Manager) Active() bool

Active reports whether the table is managed.

func (*Manager) Entries

func (m *Manager) Entries(now time.Time) int

Entries returns the number of ban elements the kernel still holds at now.

func (*Manager) LimitsStatus added in v1.6.0

func (m *Manager) LimitsStatus() (bool, string)

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

func (m *Manager) Start(ctx context.Context) error

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) Stop

func (m *Manager) Stop(ctx context.Context) error

Stop deletes the table (on agent shutdown).

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).

type PortRange added in v1.6.0

type PortRange struct {
	Start, End uint16
}

PortRange is the ports Start..End.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL