Documentation
¶
Overview ¶
Package hostfw keeps the paths plexd forwards open in the host firewall.
On Linux plexd forwards packets through the kernel's forward hook on four paths: a border Node's transit between peers of an Area, bridge mode's access routes, user access and site-to-site tunnels. nftables passes a packet only when every base chain on a hook accepts it, so the accept in plexd's own forward chain ends only that chain: a forward base chain of another table, such as Docker's ip filter FORWARD with policy drop, still drops the packet. The guard (guard_linux.go) finds those chains, opens each path with an accept where the host firewall takes its operator's rules, and reports what it may not or cannot open.
This file is built on every platform, so the bridge managers can hand their paths to a Paths whatever the platform; only Linux builds a guard.
Index ¶
Constants ¶
const DefaultInterval = 30 * time.Second
DefaultInterval is the time between two interval checks when Config.Interval is 0: the default heartbeat interval.
Variables ¶
var ErrLegacyMalformed = errors.New("malformed table")
ErrLegacyMalformed is wrapped by the error of a legacy filter table the guard cannot decode: a blob that ends inside an entry, or an offset that points outside it.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// PolicyEnabled is policy.enabled. Without plexd's forward chain behind
// an accept, nothing of plexd's filters what passes, so with false the
// guard opens nothing and only reports, with reason policy_disabled.
PolicyEnabled bool
// Open is policy.open_host_firewall. With false the guard opens nothing
// and reports with reason opening_disabled.
Open bool
// Interval is the time between two interval checks, which also catch
// changes of the legacy table; 0 reads as DefaultInterval.
Interval time.Duration
// InNetns runs every netlink and legacy call; nil calls them directly.
// The real-kernel tests point it at a network namespace, as
// nettest.Netns.Do does.
InNetns func(func() error) error
}
Config configures a Guard.
type Guard ¶
type Guard struct {
// contains filtered or unexported fields
}
Guard keeps the paths plexd forwards open in the host firewall and reports the directions it may not or cannot open. It is safe for concurrent use: Add, Remove and Blocked may be called from any goroutine, and Run checks the firewall on its own.
A check lists the foreign forward base chains (nftables_linux.go) and the legacy filter table (legacy_linux.go), evaluates every path against each of them (eval.go), and for each blocked direction:
- with PolicyEnabled false, reports its first blocker with policy_disabled;
- otherwise, with Open false, reports it with opening_disabled;
- otherwise wants plexd's accept at the head of every nftables blocker's target, and reports the first blocker that remains: a legacy one as legacy_backend, an nftables one whose insert failed as open_failed.
It then deletes every plexd rule that is not wanted and duplicates of a wanted one, and inserts every wanted rule that is missing. A wanted rule that exists is left alone, so a plexd that was killed and starts again leaves no gap, unless a rule above it drops its direction: then it goes back to the head of its chain.
The log lines are transitions only: Info "host firewall opened for forwarding" for each path an inserted accept opens, Info "host firewall accept removed" for each deleted one, Warn "host firewall drops forwarded traffic" when a direction enters the report or its entry changes, with the rule an operator would add, and Info "host firewall no longer drops forwarded traffic" when it leaves the report while its path is still wanted. They carry component=hostfw.
func New ¶
New returns a guard with an empty set; Run starts its checks. Its log lines carry component=hostfw.
func (*Guard) Add ¶
Add adds paths to the set. A path with an empty In or Out is ignored, because its rule would match every interface. A change of the set makes Run check with trigger paths. After Close, Add does nothing.
func (*Guard) Blocked ¶
func (g *Guard) Blocked() []api.ForwardingBlocked
Blocked returns a copy of the last report: nil before the first check and while nothing is blocked.
func (*Guard) Close ¶
Close makes Run return, waits for it, then deletes every plexd accept in one final pass. A failure there is returned as "hostfw: close: %w". After Close, Add and Remove do nothing; a second Close returns nil.
func (*Guard) Remove ¶
Remove removes paths from the set; a path not in it changes nothing. A change of the set makes Run check with trigger paths. After Close, Remove does nothing.
func (*Guard) Run ¶
Run checks the firewall until ctx ends or Close is called: once with trigger start, which sweeps the accepts a killed run left, then with trigger paths after every change of the set, nftables_change 1 s after the first nftables change event of a burst, and interval every Config.Interval. While the set is empty no check runs, except the one right after it became empty.
It subscribes to nftables change events and keeps only table, chain and rule events of families ip and inet whose table is not plexd's own, so plexd's own policy applies never trigger a check. A failed subscribe logs Warn "host firewall change watch unavailable", once per run of failures, and is retried at each interval tick. A closed event channel, which also means changes were missed, logs Warn "host firewall change watch stopped", checks at once and subscribes again at the next tick.
Run returns ctx.Err() when ctx ends and nil after Close. A second Run, and a Run after Close, return nil at once.
type Path ¶
Path is one direction of a path plexd forwards: packets that arrive on In and leave through Out. Kind is one of the api.ForwardingPath constants, and ID the provider ID of a user_access path or the tunnel ID of a site_to_site path, empty otherwise. A path that goes both ways is two Paths.
type Paths ¶
Paths is the set of paths a guard keeps open. It is keyed by all four fields of a Path, so two kinds that forward between the same interfaces are two members, and they share one accept rule.
Add ignores a path with an empty In or Out, because its rule would match every interface. Remove of a path not in the set does nothing. Both are safe for concurrent use and never block on the firewall: the guard checks the firewall on its own goroutine.